Article Directory
I often use stream to operate, but I will forget it if I don’t use it for a while, so I will write a blog~
List and Map conversion
List is converted to something like Map<Integer, List>
group by an attribute
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//提取名字相同的记录,拼接成map
Map<String, User> map = list.stream().collect(
Collectors.groupingBy(User::getName));
}
List converted to something like Map<Integer, Object>
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//防止key值冲突
Map<String, User> map = list.stream().collect(Collectors.toMap(
e -> {
return (e.getName() + "_" + e.getAge());
}, Function.identity(), (key1, key2) -> key2)
);
}
There are two ways of writing here:
- Collectors.toMap(User::getName, User -> User);
- Collectors.toMap(User::getName, Function.identity());
List is converted to something like Map<property1, property2>
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//两个属性值,一个为key,一个为value
Map<String, Integer> map = list.stream().collect(
Collectors.toMap(User::getName, User::getAge));
}
List<object> to List<property>
The User entity class used in the example
//定义
public class User {
private String name;
private Integer age;
private Integer gender;
}
Extract the collection or deduplication of a single attribute in an object
In the following case, extract the collection User对象
in the collection属性name
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//提取name集合
List<String> nameList = list.stream().map(User::getName).collect(Collectors.toList());
//提取name集合并去重,distinct()
List<String> nameList = list.stream().map(User::getName).distinct().collect(Collectors.toList());
}
Group by attribute to Map
//伪代码
public static void main(String[] args) {
List<User> list = getUserList();
//提取name集合
Map<String, List<User>> ageMap = list.stream().collect(
Collectors.groupingBy(User::getAge));
}
Filter an attribute and return a value
Take a chestnut: Get the name of a user whose age is 16 from the list (random)
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class TestService {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
User user1 = new User("lizzy", 18);
userList.add(user1);
User user2 = new User("小芳", 16);
userList.add(user2);
User user3 = new User("小花", 17);
userList.add(user3);
User user4 = new User("小玲", 19);
userList.add(user4);
User user5 = new User("小雨", 20);
userList.add(user5);
Optional<String> name = userList.stream().filter(user -> user.getAge() == 16).map(User::getName).findFirst();
if (name.isPresent()) {
System.out.println(String.format("年龄16岁的小姐姐芳名为:%s", name.get()));
}
}
}
Operations such as summation/average of a certain field in List
IntSummaryStatistics, and correspondingly DoubleSummaryStatistics, LongSummaryStatistics
public class SummaryStatistics {
public static void main(String[] args) {
//1.对int类型list进行求和
List<User> list = new ArrayList<User>();
list.add(new User('lizzy', 18));
list.add(new User('huajuan', 28));
IntSummaryStatistics summaryStatistics = list.stream().mapToInt((s) -> s.getAge).summaryStatistics();
System.out.println("总和:" + summaryStatistics.getSum());
System.out.println("平均数:" + summaryStatistics.getAverage());
System.out.println("总个数:" + summaryStatistics.getCount());
System.out.println("最大值:" + summaryStatistics.getMax());
System.out.println("最小值:" + summaryStatistics.getMin());
}
}
stepped on pit
foreach out of order
需要在并行流中保证元素顺序消费需要使用java.util.stream.Stream#forEachOrdered
To be continued. . .