<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>
正例:
1 2 3 4 5 6 7 8 9 10 11
<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>
//初始化list,往list 中添加元素反例: int[] arr = newint[]{1,2,3,4}; List<Integer> list = new ArrayList<>(); for (int i : arr){ list.add(i); }
正例:
1 2 3 4 5 6 7
//初始化list,往list 中添加元素正例: int[] arr = newint[]{1,2,3,4}; //指定集合list 的容量大小 List<Integer> list = new ArrayList<>(arr.length); for (int i : arr){ list.add(i); }
//频繁调用Collection.contains() 反例 List<Object> list = new ArrayList<>(); for (int i = 0; i <= Integer.MAX_VALUE; i++){ //时间复杂度为O(n) if (list.contains(i)) System.out.println("list contains "+ i); }
正例:
1 2 3 4 5 6 7 8 9
//频繁调用Collection.contains() 正例 List<Object> list = new ArrayList<>(); Set<Object> set = new HashSet<>(); for (int i = 0; i <= Integer.MAX_VALUE; i++){ //时间复杂度为O(1) if (set.contains(i)){ System.out.println("list contains "+ i); } }
七、使用静态代码块实现赋值静态成员变量
对于集合类型的静态成员变量,应该使用静态代码块赋值,而不是使用集合实现来赋值。
反例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//赋值静态成员变量反例 privatestatic Map<String, Integer> map = new HashMap<String, Integer>(){ { map.put("Leo",1); map.put("Family-loving",2); map.put("Cold on the out side passionate on the inside",3); } }; privatestatic List<String> list = new ArrayList<>(){ { list.add("Sagittarius"); list.add("Charming"); list.add("Perfectionist"); } };
正例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//赋值静态成员变量正例 privatestatic Map<String, Integer> map = new HashMap<String, Integer>(); static { map.put("Leo",1); map.put("Family-loving",2); map.put("Cold on the out side passionate on the inside",3); }
privatestatic List<String> list = new ArrayList<>(); static { list.add("Sagittarius"); list.add("Charming"); list.add("Perfectionist"); }