工具
处理集合问题,如果开发环境是Java 8+,多使用Stream,Stream 是用函数式编程方式在集合类上进行复杂操作的工具。
- 转Map
Map<String, String> collect = list.stream().collect(Collectors.toMap(obj -> obj.getXXX(), obj -> obj.getYYY()));
- 的Key,Value翻转
Map<String, String> newMap = map.entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue(), entry -> entry.getKey()));
- 将对象的某个属性拼成字符串
String collectStr = list.stream().filter(obj -> obj.getAge() > = 18).collect(Collectors.joining(","));