当前位置:网站首页>Stream collectors usage
Stream collectors usage
2022-06-30 00:15:00 【linsa_ pursuer】
//Map To List
Map<Integer, String> map = new HashMap<>();
map.put(1, "hello");
map.put(2, "linsa");
List<Integer> listSimpleKey = new ArrayList<>();
List<String> listSimpleValue = new ArrayList<>();
for (Map.Entry<Integer, String> mapTemp : map.entrySet()) {
listSimpleKey.add(mapTemp.getKey());
listSimpleValue.add(mapTemp.getValue());
}
listSimpleKey.forEach(System.out :: println);
// 1
// 2
listSimpleValue.forEach(System.out :: println);
// hello
// linsa
List<Integer> list = new ArrayList(map.keySet());
list.forEach(System.out :: println);
// 1
// 2
List<String> list1 = new ArrayList(map.values());
list1.forEach(System.out :: println);
// hello
// linsa
List<Integer> list3 = map.keySet().stream().collect(Collectors.toList());
list3.forEach(System.out :: println);
// 1
// 2
List<String> list4 = map.values().stream().collect(Collectors.toList());
list4.forEach(System.out :: println);
// hello
// linsa
// hold map And meet the requirements value convert to list
List<String> list5 = map.values().stream()
.filter(x -> !"linsa".equalsIgnoreCase(x))
.collect(Collectors.toList());
list5.forEach(System.out :: println);
// hello
// hold map And meet the requirements value Corresponding key convert to list
List<Integer> list6 = map.entrySet().stream()
.filter(x -> "linsa".equals(x.getValue()))
.map(Map.Entry::getKey).collect(Collectors.toList());
list6.forEach(System.out :: println);
// 2
// hold list in bean The field changes to list or map
List<Person> personList = new ArrayList<>();
Person person1 = new Person();
person1.setNum("111");
person1.setName("Linsa");
person1.setAge(18);
personList.add(person1);
Person person2 = new Person();
person2.setNum("222");
person2.setAge(16);
personList.add(person2);
Person person3 = new Person();
person3.setNum("333");
person3.setName("Rose");
person3.setAge(16);
personList.add(person3);
Person person4 = new Person();
person4.setNum("111");
person4.setName("Lily");
person4.setAge(18);
personList.add(person4);
Person person5 = new Person();
person5.setNum("444");
person5.setName("Clove");
person5.setAge(18);
personList.add(person5);
Person person6 = new Person();
person6.setNum("111");
person6.setName("Jasmine");
person6.setAge(17);
personList.add(person6);
// hold bean A value in is changed to list
List<String> numAllList = personList.stream().map(Person::getNum).collect(Collectors.toList());
numAllList.forEach(System.out :: println);
// 111
// 222
// 333
// 111
// 444
// 111
// hold list convert to map( Write in the first way , If a null value exists, an exception will be thrown ,1.9 This problem has been solved ,1.8 In the second way )
/*Map<String,String> map1 = personList.stream().collect(Collectors.toMap(Person::getNum, Person::getName, (k1, k2) -> k1));
System.out.println(map1);*/
Map<String,String> map2 = personList.stream().collect(HashMap::new,(m,v)->m.put(v.getNum(),v.getName()),HashMap::putAll);
System.out.println(map2);
//{111=Jasmine, 222=null, 333=Rose, 444=Clove}
//Collectors.groupingBy Group items in a collection according to one or more attributes
// Single attribute
Map<String, List<Person>> groupingByMap1 = personList.stream().collect(Collectors.groupingBy(Person::getNum));
groupingByMap1.forEach((x, y) -> System.out.println(x + y));
//111[Person{num='111', name='Linsa', age=18}, Person{num='111', name='Lily', age=18}, Person{num='111', name='Jasmine', age=17}]
//222[Person{num='222', name='null', age=16}]
//333[Person{num='333', name='Rose', age=16}]
//444[Person{num='444', name='Clove', age=18}]
// Multiple attributes
Map<String, List<Person>> groupingByMap2 = personList.stream()
.collect(Collectors.groupingBy(item -> item.getNum() + "_" + item.getAge()));
groupingByMap2.forEach((x, y) -> System.out.println(x + y));
//111_18[Person{num='111', name='Linsa', age=18}, Person{num='111', name='Lily', age=18}]
//111_17[Person{num='111', name='Jasmine', age=17}]
//444_18[Person{num='444', name='Clove', age=18}]
//222_16[Person{num='222', name='null', age=16}]
//333_16[Person{num='333', name='Rose', age=16}]
// Single conditional grouping
Map<String, List<Person>> groupingByMap3 = personList.stream().collect(Collectors.groupingBy(item -> {
if(item.getAge() < 18) {
return "18";
}else {
return "other";
}
}));
groupingByMap3.forEach((x, y) -> System.out.println(x + y));
//other[Person{num='111', name='Linsa', age=18}, Person{num='111', name='Lily', age=18}, Person{num='444', name='Clove', age=18}]
//18[Person{num='222', name='null', age=16}, Person{num='333', name='Rose', age=16}, Person{num='111', name='Jasmine', age=17}]
// Multi conditional grouping
Map<String, Map<String, List<Person>>> groupingByMap4 = personList.stream()
.collect(Collectors.groupingBy(Person::getNum, Collectors.groupingBy(item -> {
if(item.getAge() < 18) {
return "18";
}else {
return "other";
}
})));
groupingByMap4.forEach((x, y) -> System.out.println(x + y));
//111{other=[Person{num='111', name='Linsa', age=18}, Person{num='111', name='Lily', age=18}], 18=[Person{num='111', name='Jasmine', age=17}]}
//222{18=[Person{num='222', name='null', age=16}]}
//333{18=[Person{num='333', name='Rose', age=16}]}
//444{other=[Person{num='444', name='Clove', age=18}]}
//Collectors.counting Find the total
Map<String, Long> countingMap = personList.stream().collect(Collectors.groupingBy(Person::getNum, Collectors.counting()));
countingMap.forEach((x, y) -> System.out.println(x + " : " + y));
//111 : 3
//222 : 1
//333 : 1
//444 : 1
//Collectors.summingInt Sum up
Map<String, Integer> summingIntMap = personList.stream()
.collect(Collectors.groupingBy(Person::getNum, Collectors.summingInt(Person::getAge)));
summingIntMap.forEach((x, y) -> System.out.println(x + " : " + y));
//111 : 53
//222 : 16
//333 : 16
//444 : 18
//Collectors.maxBy For maximum Comparator.comparingInt
Optional<Person> maxByOp = personList.stream().collect(Collectors.maxBy(Comparator.comparingInt(Person::getAge)));
System.out.println(maxByOp.get());
//Person{num='111', name='Linsa', age=18}
//Collectors.minBy For the minimum Comparator.comparingInt
Optional<Person> minByOp = personList.stream().collect(Collectors.minBy(Comparator.comparingInt(Person::getAge)));
System.out.println(minByOp.get());
//Person{num='222', name='null', age=16}
//Collectors.collectingAndThen Grouping takes the maximum value
Map<String, Person> collectingAndThenMap = personList.stream()
.collect(Collectors.groupingBy(Person::getNum, Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparingInt(Person::getAge)), Optional::get)));
collectingAndThenMap.forEach((x, y) -> System.out.println(x + y));
//111Person{num='111', name='Linsa', age=18}
//222Person{num='222', name='null', age=16}
//333Person{num='333', name='Rose', age=16}
//444Person{num='444', name='Clove', age=18}
//Collectors.mapping Group job No. takes the name
Map<String, Set<String>> mappingMap = personList.stream()
.collect(Collectors.groupingBy(Person::getNum, Collectors.mapping(Person::getName, Collectors.toSet())));
mappingMap.forEach((x, y) -> System.out.println(x + y));
//111[Jasmine, Lily, Linsa]
//222[null]
//333[Rose]
//444[Clove]
//Collectors.groupingByConcurrent grouping - Thread safety
Map<String, List<Person>> groupingByConcurrentMap = personList.stream()
.collect(Collectors.groupingByConcurrent(person -> person.getNum() + person.getAge()));
groupingByConcurrentMap.forEach((x, y) -> System.out.println(x + y));
//11118[Person{num='111', name='Linsa', age=18}, Person{num='111', name='Lily', age=18}]
//33316[Person{num='333', name='Rose', age=16}]
//44418[Person{num='444', name='Clove', age=18}]
//11117[Person{num='111', name='Jasmine', age=17}]
//22216[Person{num='222', name='null', age=16}]
//Collectors.joining Splice the elements in the order of the elements
String ids1 = personList.stream().map(x -> x.getNum()).collect(Collectors.joining());
System.out.println(ids1);
//111222333111444111
String ids2 = personList.stream().map(x -> x.getNum()).collect(Collectors.joining(","));
System.out.println(ids2);
//111,222,333,111,444,111
String ids3 = personList.stream().map(x -> x.getNum()).collect(Collectors.joining(",","first","last"));
System.out.println(ids3);
//first111,222,333,111,444,111lastpublic class Person {
String num;
String name;
int age;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"num='" + num + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
边栏推荐
- 复制带随机指针的链表[空间换时间--hash记录]
- Mysql:sql overview and database system introduction | dark horse programmer
- Cacti maximum monitoring number test
- Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
- Basic tutorial for installing monggodb in win10
- Leetcode (76) -- Minimum Covering substring
- gyctf_2020_document
- Solr基础操作16
- Embedded development: Hardware in the loop testing
- MySQL multi table query
猜你喜欢
![Copy linked list with random pointer [space for time --hash record]](/img/d9/d81e0e4f81174c61275e4affe0777a.png)
Copy linked list with random pointer [space for time --hash record]

蛇形矩阵(数组模拟方向, d代表转弯)

MySQL:SQL概述及数据库系统介绍 | 黑马程序员

Quick Pow: 如何快速求幂

js中的事件

This simple little function saves 213 hours for our production research team in half a year

FPGA Development (1) -- serial port communication

About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb

MySQL functions and constraints

西门子低代码 9.14版本: 满足不同需求
随机推荐
视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
Vulnhub靶机-MoriartyCorp
AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
Golang6 reflection
Solr basic operations 9
QT learning 04 Hello QT
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
[advanced C language] special user-defined type
Table responsive layout tips for super nice
Clone undirected graph [bfs accesses each edge but not only nodes]
Copy linked list with random pointer [space for time --hash record]
gyctf_ 2020_ document
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
Exploration and Practice on the future direction of byte cloud database
JS绘制极坐标颜色渐变
蛇形矩阵(数组模拟方向, d代表转弯)
剑指 Offer II 035. 最小时间差
Digital collection of cultural relics, opening a new way of cultural inheritance
Solr基础操作16
如何实现搜索引擎中的拼写纠错功能——思路