<!--反例--><selectid="queryBookInfo"parameterType="com.tjt.platform.entity.BookInfo"resultType="java.lang.Integer"> select count(*) from t_rule_BookInfo t
where 1=1
<iftest="title !=null and title !='' "> AND title = #{title}
</if><iftest="author !=null and author !='' "> AND author = #{author}
</if></select><!--正例--><selectid="queryBookInfo"parameterType="com.tjt.platform.entity.BookInfo"resultType="java.lang.Integer">select count(*) from t_rule_BookInfo t
<where><iftest="title !=null and title !='' "> title = #{title}
</if><iftest="author !=null and author !='' "> AND author = #{author}
</if></where></select>
迭代entrySet() 获取Map 的key 和value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Map 获取value 反例:HashMap<String,String>map=newHashMap<>();for(Stringkey:map.keySet()){Stringvalue=map.get(key);}// Map 获取key & value 正例:for(Map.Entry<String,String>entry:map.entrySet()){Stringkey=entry.getKey();Stringvalue=entry.getValue();}
使用Collection.isEmpty() 检测空
Collection.size() 方法实现的时间复杂度可能是O(n)
任何Collection.isEmpty() 实现的时间复杂度都是O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 反例LinkedList<Object>collection=newLinkedList<>();if(collection.size()==0){System.out.println("collection is empty.");}// 正例if(collection.isEmpty()){System.out.println("collection is empty.");}// 检测是否为null 可以使用CollectionUtils.isEmpty()if(CollectionUtils.isEmpty(collection)){System.out.println("collection is null.");}
// 对于集合类型的静态成员变量,应该使用静态代码块赋值,而不是使用集合实现来赋值。// 赋值静态成员变量反例Map<String,Integer>map=newHashMap<String,Integer>(){{map.put("Leo",1);map.put("Family-loving",2);map.put("Cold on the out side passionate on the inside",3);}};List<String>list=newArrayList<>(){{list.add("Sagittarius");list.add("Charming");list.add("Perfectionist");}};//赋值静态成员变量正例Map<String,Integer>map=newHashMap<String,Integer>();static{map.put("Leo",1);map.put("Family-loving",2);map.put("Cold on the out side passionate on the inside",3);}List<String>list=newArrayList<>();static{list.add("Sagittarius");list.add("Charming");list.add("Perfectionist");}