Map
转 List
例如把 map
的 key
, value
分别对应 Person
对象两个属性:
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
List<Person> list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
以上三种方式不同之处在于排序的处理。参考链接:
https://www.concretepage.com/java/jdk-8/java-8-convert-map-to-list-using-collectors-tolist-example
List
转 List
List<Employee> employees = persons.stream()
.filter(p -> p.getLastName().equals("l1"))
.map(p -> new Employee(p.getName(), p.getLastName(), 1000))
.collect(Collectors.toList());
List
条件过滤
User match = users.stream().filter((user) -> user.getId() == 1).findAny().get();
List
转 Map
Map<Integer, String> result = list.stream().collect(Collectors.toMap(Hosting::getId, Hosting::getName));
Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));
————————————————
版权声明:本文为CSDN博主「hgc0907」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hgc0907/article/details/80756730