Java8 实现stream将对象集合list中抽取属性集合转化为map或list
首先新建一个实体类Person
@Data publicclassPerson{ /**编码*/ privateStringcode; /**名字*/ privateStringname; publicPerson(Stringcode,Stringname){ this.code=code; this.name=name; } }
实例化三个对象放入list集合中
publicstaticvoidmain(String[]args){ Personperson1=newPerson("001","张三"); Personperson2=newPerson("002","李四"); Personperson3=newPerson("002","王五"); ListpersonList=newArrayList<>(); personList.add(person1); personList.add(person2); personList.add(person3); personList.forEach(t->System.out.println(t.toString())); }
输出结果为:
Person(code=001,name=张三)
Person(code=002,name=李四)
Person(code=002,name=王五)
1.抽取对象的code作为key,name作为value转化为map集合
方法为
privatestaticHashMaplistToMap(List personList){ return(HashMap )personList.stream() .filter(t->t.getName()!=null) .collect(Collectors.toMap(Person::getCode,Person::getName,(k1,k2)->k2)); }
filter()方法作用是过滤掉名字为空的对象,当对象的名字为null时,会出现NPE空指针异常
(k1,k2)->k2意思是遇到相同的key时取第二个值
(k1,k2)->k1意思是遇到相同的key时取第一个值
调用这个方法
HashMappersonMap=listToMap(personList); personMap.forEach((k,v)->System.out.println(k.toString()+"-"+v.toString()));
输出结果为:
001-张三
002-王五
2.抽取对象的name得到name的list集合
方法为
privatestaticListgetNameList(List personList){ returnpersonList.stream().map(Person::getName).collect(Collectors.toList()); }
调用这个方法
ListnameList=getNameList(personList); nameList.forEach(t->System.out.println(t.toString()));
输出结果为:
张三
李四
王五
补充:java8使用stream将List转成Map,或者从List对象中获取单个属性List,List中根据某个字段排序
1.学生类
importlombok.Data; @Data publicclassStudent{ privateStringstuId; privateStringname; privateStringage; privateStringsex; }
2.测试类
publicclassTest{ publicstaticvoidmain(String[]args){ //创建学生List Listlist=createStudentList(); //1.获取value为Student对象,key为学生ID的Map getStudentObjectMap(list); //2.获取value为学生姓名,key为学生ID的Map getStudentNameMap(list); //3.获取学生姓名List getStudentNameList(list); //4.List中删除学生id=1的对象 list.removeIf(student->student.getStuId().equals(1)); //5.如果StudentId为Long类型如何转? Map mapStr=list.stream().collect(Collectors.toMap(student->student.getStuId().toString(),student->JSON.toJSONString(student))); //6.根据List中Student的学生姓名排序 Collections.sort(list,(o1,o2)->{ if(o1.getName().compareTo(o2.getName())>0){ return1; }elseif(o1.getName().compareTo(o2.getName())<0){ return-1; }else{ return0; } }); //7.List遍历 List listStr=newArrayList<>(); List listStu=newArrayList<>(); listStr.forEach(studentStr->{ listStu.add(JSON.parseObject(studentStr,Student.class)); }); //List根据某个字段过滤、排序 listStu.stream() .filter(student->student.getSex().equals("女")) .sorted(Comparator.comparing(Student::getName)) .collect(Collectors.toList()); //List根据某个字段分组 Map >sexGroupMap=listStu.stream() .collect(Collectors.groupingBy(Student::getSex)); //如果Map中多个名称相同,则studentId用逗号间隔 Map studentNameIdMap=listStu.stream() .collect(toMap(Student::getName,Student::getStuId,(s,a)->s+","+a)); } publicstaticList createStudentList(){ List list=newArrayList (); Studentlily=newStudent(); lily.setStuId("1"); lily.setName("lily"); lily.setAge("14"); lily.setSex("女"); Studentxiaoming=newStudent(); xiaoming.setStuId("2"); xiaoming.setName("xiaoming"); xiaoming.setAge("15"); xiaoming.setSex("男"); list.add(lily); list.add(xiaoming); returnlist; } publicstaticMap
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。