当前位置:网站首页>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 +
'}';
}
}
边栏推荐
- js中的事件
- Solr basic operations 15
- New CorelDRAW technical suite2022 latest detailed function introduction
- Code analysis platform sonarqube actual combat
- Golang6 reflection
- Label Troubleshooting: unable to open the marked image
- Majority element ii[molar voting method for finding modes]
- Leetcode (680) -- verifying palindrome string II
- Web APIs environment object - dark horse programmer
- vim插件管理器vim-plug安装方法
猜你喜欢

FPGA Development (2) -- IIC communication

剑指 Offer II 037. 小行星碰撞

项目一:部署 LAMP ecshop电商平台

Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12

基于zfoo开发项目的一些规范

Andorid source build/envsetup. SH details to know

vsftp 与 TFTP 与 samba 与 nfs 复习

Cloud native enthusiast weekly: cool collection of grafana monitoring panels

Three postures of anti CSRF blasting
![复制带随机指针的链表[空间换时间--hash记录]](/img/d9/d81e0e4f81174c61275e4affe0777a.png)
复制带随机指针的链表[空间换时间--hash记录]
随机推荐
MySQL functions and constraints
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
Viewing splitchunks code segmentation from MPX resource construction optimization
Analysis of common vlog parameters
How to write controller layer code gracefully?
Summary of DOM knowledge points
TP5查询AND和OR条件嵌套
[advanced C language] dynamic memory management
Quick Pow: 如何快速求幂
克隆无向图[bfs访问每条边而不止节点]
modelsim的TCL脚本的define incdir命令解析
Solr basic operation 11
QT learning 01 GUI program principle analysis
DOM 知识点总结
Set up enterprise NTP time server
蛇形矩阵(数组模拟方向, d代表转弯)
QT learning 04 Hello QT
AI chief architect 9- huxiaoguang, propeller model library and industry application
JS绘制极坐标颜色渐变
Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)