沈阳免费网站制作,青岛网站开发中心,做试管的网站,企业为何选择网站推广外包?问题现象#xff1a; 最近在项目中#xff0c;有一些逻辑想用List集合的Stream流式操作来快速实现#xff0c;但由于之前没做好学习笔记和总结#xff0c;导致一时间想不起来#xff0c;只能用本方法来解决#xff0c;如下#xff1a; 可以看出来代码量是比较冗长的 最近在项目中有一些逻辑想用List集合的Stream流式操作来快速实现但由于之前没做好学习笔记和总结导致一时间想不起来只能用本方法来解决如下 可以看出来代码量是比较冗长的于是就回顾了一下List集合的Stream流式操作的相关知识点打算打一个代码优化 问题分析 由上图可以知道我是想将集合listListMapString, Object list 根据元素MapString, Object map中的key为infoType的字段值来作相关的业务逻辑形成指定的集合如ListMapString, Object baseInfoList等然后再存到map集合MapString, Object exportParams中去。 根据这个思路其实就可以使用集合list中的Stream流式操作中的Collectors.groupingBy方法来解决了 1、首先对集合list中使用Stream流式操作中的Collectors.groupingBy进行分组分组依据是元素中的keyinfoType形成infoTypeListMap集合MapString, ListMapString, Object infoTypeListMap。 2、遍历infoTypeListMap集合然后将元素存入exportParams集合。 解决方法 将上图的代码做如下修改即可 exportParams.put(StrUtil.toCamelCase(StaffInfoTypeEnum.BASE_INFO.getCode() _LIST), new ArrayList());exportParams.put(StrUtil.toCamelCase(StaffInfoTypeEnum.EDUCATION_INFO.getCode() _LIST), new ArrayList());exportParams.put(StrUtil.toCamelCase(StaffInfoTypeEnum.TRAIN_INFO.getCode() _LIST), new ArrayList());exportParams.put(StrUtil.toCamelCase(StaffInfoTypeEnum.WORK_EXPERIENCE_INFO.getCode() _LIST), new ArrayList());exportParams.put(StrUtil.toCamelCase(StaffInfoTypeEnum.SALARY_INFO.getCode() _LIST), new ArrayList());exportParams.put(StrUtil.toCamelCase(StaffInfoTypeEnum.FAMILY_CONTACT_INFO.getCode() _LIST), new ArrayList());exportParams.put(StrUtil.toCamelCase(StaffInfoTypeEnum.LANGUAGE_INFO.getCode() _LIST), new ArrayList());MapString, ListMapString, Object infoTypeListMap list.stream().collect(Collectors.groupingBy(map - (String)map.get(infoType)));infoTypeListMap.entrySet().stream().forEach(entry-{String key entry.getKey();ListMapString, Object mapList entry.getValue();exportParams.put(StrUtil.toCamelCase(key_LIST), mapList);}); 拓展 文末有我写的测试代码有助于理解可直接搬运使用 相信小伙伴们都见识到List集合的Stream流式操作的强大了吧接下来我就把List集合的Stream流式操作实现数据类型转换的常用方法记录并分享在此以供自己和大家学习记忆。 1、Collectors.toList() list.stream().collect(Collectors.toList()); 作用可用于克隆/拷贝原list集合。作用相当于以下代码
new ArrayList(list);2、Collectors.toCollection(ArrayList::new)
list.stream().collect(Collectors.toCollection(ArrayList::new)); 作用ListObject 转为 ArrayListObject 。作用相当于以下代码
new ArrayList(list);3、Collectors.toCollection(LinkedList::new)
list.stream().collect(Collectors.toCollection(LinkedList::new)); 作用ListObject 转为 LinkedListObject 。作用相当于以下代码
new LinkedList(list);4、Collectors.toCollection(LinkedHashSet::new)
list.stream().collect(Collectors.toCollection(LinkedHashSet::new)); 作用ListObject 转为 LinkedHashSetObject。作用相当于以下代码
new LinkedHashSet(list);5、Collectors.toCollection(HashSet::new)
list.stream().collect(Collectors.toCollection(HashSet::new));作用ListObject 转为 HashSetObject 。作用相当于以下代码
new HashSet(list);6、Collectors.toCollection(TreeSet::new)
list.stream().collect(Collectors.toCollection(TreeSet::new)); 作用ListObject 转为 TreeSetObject。 7、Collectors.partitioningBy
list.stream().collect(Collectors.partitioningBy(s - s.length() 2)); 作用ListObject 转为 MapBoolean, ListObject。 8、【重点】Collectors.groupingBy
list.stream().collect(Collectors.groupingBy(s - s)); 作用ListObject 转为 MapObject, ListObject。 9、Collectors.collectingAndThen
list.stream().collect(Collectors.groupingBy(s - s)); 作用根据指定条件将集合中所有元素形成的指定类型的结果集合后再将结果集合做指定的结果集。如ListObject 转为 MapObject, ListObject 再转为 SetString返回如下
list.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(s - s), Map::keySet)); 10、map
list.stream().collect(Collectors.groupingBy(s - s)); 作用根据指定条件提取集合中所有元素的指定属性/字段形成新的类型指定属性/字段的数据类型集合。如ListObject 转为 ListInteger。
list.stream().map(String::length).collect(Collectors.toList()); 11、【重点】Collectors.toMap 作用ListObject 转为 MapObject, Object。 具有3个重载方法 11.1、Collectors.toMapkey, value)
list.stream().collect(Collectors.toMap(str - str, String::length)); 作用根据指定条件提取集合中所有元素的指定属性/字段形成新的类型指定属性/字段的数据类型集合。 参数说明 key指定元素对象的某个属性作为map结果集合的key值。 value指定元素对象的某个属性作为map结果集合的value值。 缺点当元素中存在重复的key时会有如下报错Duplicate key XX。 11.2、【重点】Collectors.toMapkey, value, distinctStrategy)
list.stream().collect(Collectors.toMap(String::length, str - str, (length, str) - str)); 作用在11.1功能一致多了一个第三参数该参数用于配置当出现重复key时对这些元素的value的操作处理逻辑可以避免key重复报错问题。 参数说明 distinctStrategy去重逻辑当遇到重复key时触发该逻辑。 11.3、【重点】Collectors.toMapkey, value, distinctStrategy, returnTypeSupplier)
list.stream().collect(Collectors.toMap(String::length, str - str, (length, str) - str, TreeMap::new)); 参数说明 returnTypeSupplier指定map结果集合的数据类型通过查询源代码可知当未指定该参数时默认返回的是HashMap数据类型如下 测试代码 1、StreamStringListTransformTest 测试类 测试ListString集合的Stream流式操作实现数据类型转换的功能
import java.util.*;
import java.util.stream.Collectors;/*** Stream流的各种数据类型转换操作*/
public class StreamStringListTransformTest {public static void main(String[] args) {//ListString集合原数据ListString list Arrays.asList(java, python, C#,php);//1、Collectors.toList()// ListString克隆代替流ListString listResult list.stream().collect(Collectors.toList());listResult.forEach(System.out::println);System.out.println(--------------);//2、Collectors.toCollection(ArrayList::new)// ListString 转为 ArrayListStringArrayListString arrayList list.stream().collect(Collectors.toCollection(ArrayList::new));arrayList.forEach(System.out::println);System.out.println(--------------);//3、Collectors.toCollection(LinkedList::new)// ListString 转为 LinkedListStringListString linkedList list.stream().collect(Collectors.toCollection(LinkedList::new));linkedList.forEach(System.out::println);System.out.println(--------------);//4、Collectors.toCollection(LinkedHashSet::new)// ListString 转为 LinkedHashSetStringLinkedHashSetString linkedHashSet list.stream().collect(Collectors.toCollection(LinkedHashSet::new));linkedHashSet.forEach(System.out::println);//LinkedHashSet是有序的System.out.println(--------------);//5、Collectors.toCollection(HashSet::new)// ListString 转为 HashSetStringHashSetString hashSet list.stream().collect(Collectors.toCollection(HashSet::new));hashSet.forEach(System.out::println);//HashSet是无序的按hash逻辑自动排序System.out.println(--------------);//6、Collectors.toCollection(TreeSet::new)// ListString 转为 TreeSetStringTreeSetString treeSet list.stream().collect(Collectors.toCollection(TreeSet::new));treeSet.forEach(System.out::println);//TreeSet是按自然顺序自动排序System.out.println(--------------);//7、Collectors.partitioningBy根据指定条件将集合中所有元素分成key为true或false的两组集合形成的map集合// ListString 转为 MapBoolean, ListStringMapBoolean, ListString partitioningByMap list.stream().collect(Collectors.partitioningBy(s - s.length() 2));System.out.println(partitioningByMap.toString());System.out.println(--------------);//8、Collectors.groupingBy根据指定条件将集合中所有元素分成key为元素本身的集合形成的map集合//ListString 转为 MapString, ListStringMapString, ListString groupingByMap list.stream().collect(Collectors.groupingBy(s - s));System.out.println(groupingByMap.toString());System.out.println(--------------);//9、Collectors.collectingAndThen根据指定条件将集合中所有元素形成的指定类型的结果集合后再将结果集合做指定的结果集//ListString 转为 MapString, ListString 再转为 SetStringSetString collectingAndThen list.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(s - s), Map::keySet));System.out.println(collectingAndThen.toString());System.out.println(--------------);//10、map根据指定条件提取集合中所有元素的指定属性形成新的指定集合//ListString 转为 ListIntegerListInteger lengthList list.stream().map(String::length).collect(Collectors.toList());System.out.println(lengthList.toString());System.out.println(--------------);//11、Collectors.toMap根据指定条件提取集合中所有元素的指定属性组合成自定义类型的map结果集合//ListString 转为 MapInteger, String//ListString 转为 MapString, Integer//注意该函数有3个重载方法// 11.1、2个参数(key,value)// 第一个参数元素对象的某个指定属性作为key。// 第二个参数作为value。缺点当存在key重复的不同元素时会有类似以下报错Duplicate key 王五// 11.2、3个参数(key,value,distinctStrategy)// 第一个参数元素对象的某个指定属性作为key// 第二个参数作为value// 第三个参数用于配置当出现重复key时对这些元素的value的操作处理逻辑可以避免上面的key重复报错问题。// 11.2、3个参数(key,value,distinctStrategy,returnTypeSupplier)// 第一个参数元素对象的某个指定属性作为key// 第二个参数作为value// 第三个参数用于配置当出现重复key时对这些元素的value的操作处理逻辑可以避免上面的key重复报错问题。// 第四个参数用于设置返回map的数据类型如TreeMap、ConcurrentHashMap等默认是返回HashMap。ListString strList Arrays.asList(java, python, C#,php, java);//ListString 转为 MapInteger, String
// MapInteger, String strList1 strList.stream().collect(Collectors.toMap(String::length, str - str));//报错Duplicate key java
// System.out.println(strList1.toString());
// System.out.println(--------------);//ListString 转为 MapString, Integer
// MapString, Integer strList2 strList.stream().collect(Collectors.toMap(str - str, String::length));//报错Duplicate key 4
// System.out.println(strList2.toString());
// System.out.println(--------------);//ListString 转为 MapString, IntegerMapString, Integer strList3 strList.stream().collect(Collectors.toMap(str - str, String::length, (first, second) - second));System.out.println(strList3.toString());System.out.println(--------------);MapString, Integer list1 list.stream().collect(Collectors.toMap(str - str, String::length));System.out.println(list1.toString());System.out.println(--------------);//ListString 转为 MapString, IntegerMapInteger, String list2 list.stream().collect(Collectors.toMap(String::length, str - str));System.out.println(list2.toString());System.out.println(--------------);//ListString 转为 MapString, IntegerMapInteger, String list3 list.stream().collect(Collectors.toMap(String::length, str - str, (length, str) - str));System.out.println(list3.toString());System.out.println(--------------);//ListString 转为 TreeMapString, IntegerTreeMapInteger, String list4 list.stream().collect(Collectors.toMap(String::length, str - str, (length, str) - str, TreeMap::new));System.out.println(list4.toString());System.out.println(--------------);}
} 2、Person实体类 用于支持的测试
public class Person {private String name;private Integer age;public Person() {}public Person(String name, Integer age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}Overridepublic String toString() {return Person{ name name \ , age age };}
} 3、StreamObjectListTransformTest 测试类 测试ListObject集合的Stream流式操作实现数据类型转换的功能
import xxx.Person;//注意修改为引用的Person实体类的文件路径import java.util.*;
import java.util.stream.Collectors;/*** Stream流的各种数据类型转换操作*/
public class StreamObjectListTransformTest {public static void main(String[] args) {//ListObject集合原数据ListPerson list new ArrayList();list.add(new Person(老六, 20));list.add(new Person(王五, 20));list.add(new Person(李四, 19));list.add(new Person(张三, 18));list.add(new Person(钱二, 17));list.add(new Person(赵一, 16));//1、Collectors.toList()// ListObject克隆代替流ListPerson listResult list.stream().collect(Collectors.toList());listResult.forEach(System.out::println);System.out.println(--------------);//2、Collectors.toCollection(ArrayList::new)// ListObject 转为 ArrayListObjectArrayListPerson arrayList list.stream().collect(Collectors.toCollection(ArrayList::new));arrayList.forEach(System.out::println);System.out.println(--------------);//3、Collectors.toCollection(LinkedList::new)// ListObject 转为 LinkedListObjectListPerson linkedList list.stream().collect(Collectors.toCollection(LinkedList::new));linkedList.forEach(System.out::println);System.out.println(--------------);//4、Collectors.toCollection(LinkedHashSet::new)// ListObject 转为 LinkedHashSetObjectLinkedHashSetPerson linkedHashSet list.stream().collect(Collectors.toCollection(LinkedHashSet::new));linkedHashSet.forEach(System.out::println);//LinkedHashSet是有序的System.out.println(--------------);//5、Collectors.toCollection(HashSet::new)// ListObject 转为 HashSetObjectHashSetPerson hashSet list.stream().collect(Collectors.toCollection(HashSet::new));hashSet.forEach(System.out::println);//HashSet是无序的按hash逻辑自动排序System.out.println(--------------);// //6、Collectors.toCollection(TreeSet::new)
// // ListObject 转为 TreeSetObject
// TreeSetPerson treeSet list.stream().collect(Collectors.toCollection(TreeSet::new));
// treeSet.forEach(System.out::println);
//TreeSet是按单一元素自然顺序自动排序所以转换时会有类似以下报错
// com.stephen.javademo.stream.bo.Person cannot be cast to java.lang.Comparable
// System.out.println(--------------);//7、Collectors.partitioningBy根据指定条件将集合中所有元素分成key为true或false的两组集合形成的map集合// ListObject 转为 MapBoolean, ListObjectMapBoolean, ListPerson partitioningByMap list.stream().collect(Collectors.partitioningBy(person - person.getAge() 18));System.out.println(partitioningByMap.toString());System.out.println(--------------);//8、Collectors.groupingBy根据指定条件将集合中所有元素分成key为元素本身的集合形成的map集合//ListObject 转为 MapString, ListObjectMapString, ListPerson groupingByMap list.stream().collect(Collectors.groupingBy(Person::getName));System.out.println(groupingByMap.toString());System.out.println(--------------);//9、Collectors.collectingAndThen根据指定条件将集合中所有元素形成的指定类型的结果集合后再将结果集合做指定的结果集//ListObject 转为 MapString, ListObject 再转为 SetStringCollectionListPerson collectingAndThen list.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(Person::getName), Map::values));System.out.println(collectingAndThen.toString());System.out.println(--------------);//10、map根据指定条件提取集合中所有元素的指定属性形成新的指定集合//ListObject 转为 ListStringListString nameList list.stream().map(Person::getName).collect(Collectors.toList());System.out.println(nameList.toString());System.out.println(--------------);//ListObject 转为 ListIntegerListInteger ageList list.stream().map(Person::getAge).collect(Collectors.toList());System.out.println(ageList.toString());System.out.println(--------------);//ListObject 转为 SetIntegerSetInteger ageSet list.stream().map(Person::getAge).collect(Collectors.toSet());System.out.println(ageSet.toString());System.out.println(--------------);//11、Collectors.toMap根据指定条件提取集合中所有元素的指定属性组合成自定义类型的map结果集合//ListObject 转为 MapObject, Object//注意该函数有3个重载方法// 11.1、2个参数(key,value)// 第一个参数元素对象的某个指定属性作为key。// 第二个参数作为value。缺点当存在key重复的不同元素时会有类似以下报错Duplicate key 王五// 11.2、3个参数(key,value,distinctStrategy)// 第一个参数元素对象的某个指定属性作为key// 第二个参数作为value// 第三个参数用于配置当出现重复key时对这些元素的value的操作处理逻辑可以避免上面的key重复报错问题。// 11.2、3个参数(key,value,distinctStrategy,returnTypeSupplier)// 第一个参数元素对象的某个指定属性作为key// 第二个参数作为value// 第三个参数用于配置当出现重复key时对这些元素的value的操作处理逻辑可以避免上面的key重复报错问题。// 第四个参数用于设置返回map的数据类型如TreeMap、ConcurrentHashMap等默认是返回HashMap。//ListPerson 转为 MapInteger, String
// MapInteger, String personMap1 list.stream().collect(Collectors.toMap(Person::getAge, Person::getName));//报错Duplicate key 王五
// System.out.println(personMap1.toString());
// System.out.println(--------------);//ListPerson 转为 MapString, IntegerMapString, Integer personMap2 list.stream().collect(Collectors.toMap(Person::getName, Person::getAge));System.out.println(personMap2.toString());System.out.println(--------------);//ListPerson 转为 MapString, PersonMapString, Person personMap3 list.stream().collect(Collectors.toMap(Person::getName, person - person));System.out.println(personMap3.toString());System.out.println(--------------);//ListPerson 转为 MapInteger, StringMapInteger, String personMap4 list.stream().collect(Collectors.toMap(Person::getAge, Person::getName, (preValue, nextValue) - nextValue));//(preValue, nextValue) - nextValue)key重复时取后者System.out.println(personMap4.toString());System.out.println(--------------);//ListPerson 转为 MapInteger, StringMapInteger, String personMap5 list.stream().collect(Collectors.toMap(Person::getAge, Person::getName, (preValue, nextValue) - preValue));//(preValue, nextValue) - preValue)key重复时取前者System.out.println(personMap5.toString());System.out.println(--------------);//ListPerson 转为 MapInteger, StringMapInteger, String personMap6 list.stream().collect(Collectors.toMap(Person::getAge, Person::getName, (preValue, nextValue) - preValue、nextValue));//(preValue, nextValue) - preValue、nextValue)key重复时取两者拼接System.out.println(personMap6.toString());System.out.println(--------------);//ListPerson 转为 TreeMapInteger, StringTreeMapInteger, String personMap7 list.stream().collect(Collectors.toMap(Person::getAge, Person::getName, (preValue, nextValue) - preValue 、 nextValue, TreeMap::new));//(preValue, nextValue) - preValue、nextValue)key重复时取两者拼接System.out.println(personMap7.toString());System.out.println(--------------);}
}