Analysis of stream() method of java List

1. Introduction

Four commonly used stream() usages.

(1) The list.stream().map().collect() method can obtain a certain field of the JavaBean in the list and convert it into a new list.

(2) The list.stream().filter().map().collect() method can filter the JavaBean in the list, keep the qualified JavaBean, and then convert a certain field in the JavaBean into a new the list.

(3) The list.stream().collect() method can convert the list into a map, and can specify a field of the JavaBean in the list or the JavaBean itself as the key or value of the map. (Or customize the key and value of the map according to the situation)

(4) The list.stream().filter().collect() method can first filter out the qualified JavaBean in the list, and then convert the list into a map. You can specify a field of the JavaBean in the list or the JavaBean itself It is the key or value of the map. (Or customize the key and value of the map according to the situation)

2. Code sample

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.stream.Collectors;

public class Test {

public static void main(String[] args) {

Bean bean1 = new Bean(1, "a", "za");

Bean bean2 = new Bean(2, "b", "zb");

Bean bean3 = new Bean(3, "c", "zc");

ArrayList<Bean> list1 = new ArrayList();

list1.add(bean1);

list1.add(bean2);

list1.add(bean3);

//Get the ids of all Bean objects in list1 and put them into another list array

//Note that this .map() method returns a stream object, not a map

//.stream().map().collect()

List<Long> ids = list1.stream().map(bean -> bean.getId()).collect(Collectors.toList());

System.out.println("ids=========" + ids);

//Use the filter method to filter all Bean objects in list1, leave the Bean objects whose ids belong to ids, and convert their name fields into a list

//Delete one to see the difference

ids.remove(0);

//.stream().filter().map().collect()

List<String> names = list1.stream().filter(bean -> ids.contains(bean.getId())).map(Bean::getName).collect(Collectors.toList());

//List<String> names = list1.stream().filter(bean -> ids.contains(bean.getId())).map(bean -> bean.getName()).collect(Collectors.toList());

System.out.println("names====" + names);

//Convert all Bean objects in list1 to map, where id is the key of the map, and the Bean object itself is the value of the map

//.stream().collect()

Map<Long, Bean> maps = list1.stream().collect(java.util.stream.Collectors.toMap(Bean::getId, e -> e));

//Map<Long, String> maps = list1.stream().collect(Collectors.toMap(Bean::getId, e -> e.getName()));

System.out.println("maps====" + maps);

Set<Long> longs = maps.keySet();

System.out.println("longs====" + longs);

for (Long l : longs) {

System.out.print(l + " " + maps.get(l).getName() + " | ");

}

System.out.println();

//Generate a map after filtering according to the conditions, similar to the above filter, only keep the bean objects with ids in ids

//.stream().filter().collect()

Map<Long, Bean> maps2 = list1.stream().filter(bean -> ids.contains(bean.getId())).collect(java.util.stream.Collectors.toMap(Bean::getId, e -> e));

System.out.println(maps2);

Set<Long> longs2 = maps2.keySet();

for (Long l : longs2) {

System.out.print(l + " " + maps2.get(l).getName() + " | ");

}

System.out.println();

}

static class Bean {

private long id;

private String name;

private String address;

public Bean(long id, String name, String address) {

this.id = id;

this.name = name;

this.address = address;

}

public long getId() {

return id;

}

public void setId(long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

}

}

Guess you like

Origin blog.csdn.net/dd2016124/article/details/128724099