Java8 stream对象集合list转map或list Java8 实现stream将对象集合list中抽取属性集合转化为map或list
文耀文耀 人气:1想了解Java8 实现stream将对象集合list中抽取属性集合转化为map或list的相关内容吗,文耀文耀在本文为您仔细讲解Java8 stream对象集合list转map或list的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java8,stream,对象集合list,map转list,下面大家一起来学习吧。
首先新建一个实体类Person
@Data public class Person { /** 编码 */ private String code; /** 名字 */ private String name; public Person(String code, String name) { this.code = code; this.name = name; } }
实例化三个对象放入list集合中
public static void main(String[] args) { Person person1 = new Person("001", "张三"); Person person2 = new Person("002", "李四"); Person person3 = new Person("002", "王五"); List<Person> personList = new ArrayList<>(); 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集合
方法为
private static HashMap<String, String> listToMap(List<Person> personList) { return (HashMap<String, String>)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时取第一个值
调用这个方法
HashMap<String,String> personMap = listToMap(personList); personMap.forEach((k,v)-> System.out.println(k.toString() + " - " + v.toString()));
输出结果为:
001 - 张三
002 - 王五
2.抽取对象的name得到name的list集合
方法为
private static List<String> getNameList(List<Person> personList) { return personList.stream().map(Person::getName).collect(Collectors.toList()); }
调用这个方法
List<String> nameList = getNameList(personList); nameList.forEach(t -> System.out.println(t.toString()));
输出结果为:
张三
李四
王五
补充:java8 使用stream将List转成Map,或者从List对象中获取单个属性List,List中根据某个字段排序
1.学生类
import lombok.Data; @Data public class Student{ private String stuId; private String name; private String age; private String sex; }
2.测试类
public class Test { public static void main(String[] args) { // 创建学生List List<Student> list = 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<String, String> 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) { return 1; } else if (o1.getName().compareTo(o2.getName()) < 0) { return -1; } else { return 0; } }); //7.List遍历 List<String> listStr = new ArrayList<>(); List<Student> listStu = new ArrayList<>(); 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<String,List<Student>> sexGroupMap = listStu.stream() .collect(Collectors.groupingBy(Student::getSex)); //如果Map中多个名称相同,则studentId用逗号间隔 Map<String,String> studentNameIdMap = listStu.stream() .collect(toMap(Student::getName,Student::getStuId,(s,a)->s+","+a)); } public static List<Student> createStudentList() { List<Student> list = new ArrayList<Student>(); Student lily = new Student(); lily.setStuId("1"); lily.setName("lily"); lily.setAge("14"); lily.setSex("女"); Student xiaoming = new Student(); xiaoming.setStuId("2"); xiaoming.setName("xiaoming"); xiaoming.setAge("15"); xiaoming.setSex("男"); list.add(lily); list.add(xiaoming); return list; } public static Map<Object, Object> getStudentObjectMap(List<Student> list) { Map<Object, Object> map = list.stream().collect(Collectors.toMap(Student::getStuId, student -> student)); map.forEach((key, value) -> { System.out.println("key:" + key + ",value:" + value); }); return map; } public static Map<String, String> getStudentNameMap(List<Student> list) { Map<String, String> map = list.stream().collect(Collectors.toMap(Student::getStuId, Student::getName)); map.forEach((key, value) -> { System.out.println("key:" + key + ",value:" + value); }); return map; } public static List<String> getStudentNameList(List<Student> list) { List<String> result = list.stream().map(student -> student.getName()).collect(Collectors.toList()); for (String name : result) { System.out.println("name:" + name); } return result; } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
加载全部内容