前言
好的工具库可以简化代码,提高开发效率
一.Java自带工具库
1.比较两个对象是否相等
可以同时校验两个字符串为空,防止空指针
注意两个属性值都为null返回true
User user1 = new User;
User user2 = new User;
//before
System.out.println(user1.getName().equals(user2.getName())); //NullPointerException
//after
System.out.println(Objects.equals(user1.getName(), user2.getName())); //true
2.比较两个字符串是否相等,忽略大小写
//before
System.out.println("Abc".toUpperCase().equals("aBc".toUpperCase()));//true 手动转大写比较
//after
System.out.println("Abc".equalsIgnoreCase("aBc")); //true 查看源码内部参数转大写
3.list交差集
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
list2.add("d");
//交集
list1.retainAll(list2);
System.out.println(list1); // 输出[a, b]
//差集
list2.removeAll(list1);
System.out.println(list2); // 输出[d]
4.set交差集
set方法同list,set效率更高
Set<String> set1 = new HashSet<>();
set1.add("a");
set1.add("b");
set1.add("c");
Set<String> set2 = new HashSet<>();
set2.add("a");
set2.add("b");
set2.add("d");
//交集
set1.retainAll(set2);
System.out.println(set1); // 输出[a, b]
//差集
set2.removeAll(set1);
System.out.println(set2); // 输出[d]
5.删除集合中某个元素
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
//before
Iterator<String> iterator = list1.iterator();
if (iterator.hasNext()) {
if ("a".equals(iterator.next())) {
iterator.remove();
}
}
list1.forEach(System.out::println); // 输出[b, c]
//after
list1.removeIf("b"::equals);
list1.forEach(System.out::println); // 输出[c]
二、外部工具库
- apache commons-lang 老项目用的比较多
- hutool 是一个小而全的Java工具类库 可以去hutool官网查看介绍
本次使用hutool工具库
1.字符串判断为空
有很多工具类有判断非空的方法
还是有很多人喜欢自己写?
isEmpty 没有忽略空格参数,是以是否为空和是否存在为判断依据。
isBlank 是在 isEmpty 的基础上进行了为空(字符串都为空格、换行符、制表符、tab 的情况)的判断。(一般更为常用)
//before
String str = " ";
System.out.println(str == null || str.length() == 0);//false
//after
System.out.println(StrUtil.isEmpty(""));//true
System.out.println(StrUtil.isEmpty(" "));//false
System.out.println(StrUtil.isEmpty(null));//true
System.out.println(StrUtil.isEmpty(" abc"));//false
System.out.println(StrUtil.isBlank(""));//true
System.out.println(StrUtil.isBlank(" "));//true
System.out.println(StrUtil.isBlank(null));//true
System.out.println(StrUtil.isBlank(" abc"));//false
2.集合判断为空
List<String> list1 = null;
List<String> list2 = new ArrayList<>();
List<String> list3 = new ArrayList<>();
list3.add("a");
list3.add("b");
list3.add("c");
//before
System.out.println(list1.size() > 0);//NullPointerException
System.out.println(list2.size() > 0);//false
System.out.println(list3.size() > 0);//true
//after
System.out.println(CollectionUtil.isNotEmpty(list1));//false
System.out.println(CollectionUtil.isNotEmpty(list2));//false
System.out.println(CollectionUtil.isNotEmpty(list3));//true
三、高级操作
1.Multimap
一个key可以映射多个value的HashMap
Multimap<String, Integer> map = ArrayListMultimap.create();
map.put("key", 1);
map.put("key", 2);
System.out.println(map); //{"key":[1,2]}
//获取value
Collection<Integer> values = map.get("key");
System.out.println(values); //[1,2]
//转成集合Map
Map<String, Collection<Integer>> collectionMap = map.asMap();
System.out.println(collectionMap); //{"key":[1,2]}
2.BiMap
value不能重复的HashMap
BiMap<String, String> biMap = HashBiMap.create();
biMap.put("key", "value");
//biMap.put("key222", "value");//异常 value already present
biMap.forcePut("key2","value");//删除存在的value,重新添加
System.out.println(biMap); // 输出 {key2=value}
// 翻转key/value
BiMap<String, String> inverse = biMap.inverse();
System.out.println(inverse); // 输出 {value=key2}
持续更新中