当前位置:网站首页>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 +
'}';
}
}
边栏推荐
- TP5查询AND和OR条件嵌套
- MySQL:SQL概述及数据库系统介绍 | 黑马程序员
- Solr basic operations 14
- Cacti settings for spin polling
- Solr基础操作8
- 8软件工程环境
- Project 1: deploy lamp ECSHOP e-commerce platform
- QT learning 05 introduction to QT creator project
- Unity about failure (delay) of destroy and ondestroy
- Digital collection of cultural relics, opening a new way of cultural inheritance
猜你喜欢

Quick Pow: 如何快速求幂
![[advanced C language] address book implementation](/img/e6/8a51d519d31ec323cf04c59a556325.png)
[advanced C language] address book implementation

Set up security groups, record domain names, and apply for SSL certificates

QPainter的使用入门:绘制象棋界面
500 error occurred after importing skins folder into solo blog skin

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

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

What is IGMP? What is the difference between IGMP and ICMP?

Embedded development: Hardware in the loop testing

Fine grained identification, classification, retrieval data set collation
随机推荐
这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
Buffer flow exercise
Solr基础操作9
将日志文件存储至 RAM 以降低物理存储损耗
Machine learning: the concept and application of VC dimension
Solr basic operation 8
Virtual machine online migration based on openstack
Construction of module 5 of actual combat Battalion
Solr基础操作14
500 error occurred after importing skins folder into solo blog skin
[advanced C language] address book implementation
Solr basic operations 13
[advanced C language] dynamic memory management
Teach you step by step to install webwind notes on edge browser
Solr basic operation 16
Cacti maximum monitoring number test
旋转彩色三叶草
DOM 知识点总结
Koa2 learning and using
There is no web-based development for the reward platform. Which is suitable for native development or mixed development?