Table of contents
9. Convert List structure to Map structure
foreword
After graduating and entering the job, I started to get in touch with Stream. I found it very convenient. I recorded the method of Stream as comprehensively as possible.
1. forEach traversal
forEach: This method receives a Consumer interface function, and hands each stream element to the function for processing.
forEach method: used to traverse the data in the stream.
Note: It is a final method. After traversal, other methods in the Stream stream cannot be called
public class Stream_ForEach {
public static void main(String[] args) {
//获取一个Stream流
Stream<String>stream= Stream.of("张三","李四","王五","赵六");
//使用Stream流的方法forEach对stream流中的数据遍历
stream.forEach((String name)->{
System.out.println(name);
});
}
}
2. filter filter
filter: used to filter the data in the Stream stream
filter(Predicate<? super T>predicate)
The parameter Predicate of the filter method is a functional interface, and the abstract method boolean test (T t)
in the lambda expression Predicate can be used
public class Stream_filter {
public static void main(String[] args) {
//创建一个Stream流
Stream<String> stream = Stream.of("张三", "李四", "王五", "赵六1", "刘老七");
//单条件过滤
Stream<String> stream1 = stream.filter((String name) -> {
return name.startsWith("刘");
});
//多条件过滤
List<String> stream2 = stream.filter((String name) -> {
if(name.length()>=3 && name.equals("刘老七")){
return true;
}
return false;
}).collect(Collectors.toList);
//遍历stream1
stream1.forEach((name)-> System.out.println(name));
//输出stream2
System.out.println(stream2)
}
}
3. distinct deduplication
public class Stream_distinct {
public static void main(String[] args) {
//创建一个Stream流
Stream<String> stream = Stream.of("张三", "张三","张三","李四", "王五", "赵六1", "刘老七");
//去重
List<String> stream1 = stream().distinct().collect(Collectors.toList());
//输出stream1
System.out.println(stream1)
}
}
4. limit interception
limit: used to intercept the elements in the stream
limit can intercept the stream, only the first n
limit (long maxSize);
the parameter is a long type, if the current length of the set is greater than the parameter, it will be intercepted, otherwise no operation will be performed.
limit is a Delay method, you can continue to use the Stream method
public class Stream_limit {
public static void main(String[] args) {
//创建一个Stream流
Stream<String> stream = Stream.of("张三", "张三","张三","李四", "王五", "赵六1", "刘老七");
//去重
List<String> list = stream().distinct().collect(Collectors.toList());
//截取去重后的前2个元素
list = list.stream().limit(2).collect(Collectors.toList();
//输出stream1
System.out.println(list)
}
}
5. skip skip
skip method: used to skip elements
skip(long n)
If the current length of the stream is greater than n, skip the first n, otherwise an empty stream with a length of 0 will be obtained
public class Stream_skip {
public static void main(String[] args) {
//创建一个Stream流
Stream<String> stream = Stream.of("张三", "张三","张三","李四", "王五", "赵六1", "刘老七");
//去重
List<String> list = stream().distinct().collect(Collectors.toList());
//跳过去重后的前2个元素
list = list.stream().skip(2).collect(Collectors.toList());
//输出stream1
System.out.println(list)
}
}
6. sort sorted
public class Stream_sorted {
public static void main(String[] args) {
List<Test> list = new ArrayList<>();
list.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));
list.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));
list.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//根据年龄从大到小排序
list = list.stream()
.sorted(Comparator.comparing(Test::getAge).reversed())
.collect(Collectors.toList());
list.forEach(System.out::println);
}
}
7. Maximum value max, min
public class Stream_max_min {
public static void main(String[] args) {
List<Test> list = new ArrayList<>();
list.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));
list.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));
list.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//获取年龄最大的人
Test maxPeople = list.stream().max(Comparator.companing(Test::getAge)).get();
//获取年龄最小的人
Test minPeople = list.stream().min(Comparator.companing(Test::getAge)).get();
}
}
8. Statistics reduce
public class Stream_reduce {
public static void main(String[] args) {
List<Test> testList = new ArrayList<Test>();
testList.add(new Test("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
testList.add(new Test("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
testList.add(new Test("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//统计年龄总和
int totalAge =testList.stream().mapToInt(Test::getAge).sum();
//统计工资总和
BigDecimal totalSalary = testList.stream().map(Test::getSalary)
//统计工资乘以各自系数的总和(向上保留两位)
BigDecimal totalRatioSalary = testList.stream()
.map(s->s.getSalary()
.multiply(s.getRatio())
.setScale(2,BigDecimal.ROUND_HALF_UP))
.reduce(BigDecimal.ZERO,BigDecimal::add);
}
}
9. Convert List structure to Map structure
public class Stream_List_Map {
public static void main(String[] args) {
List<Test> testList = new ArrayList<Test>();
testList.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));
testList.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));
testList.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//根据姓名转map,map的key为name
Map<String, Test> nameMap= testList.stream().collect(Collectors.toMap(Test::getName, Test -> Test);
System.out.println(map);
}
}
10. Convert List<Object> to List<String>
public class Stream_object_string {
public static void main(String[] args) {
List<Test> testList = new ArrayList<Test>();
testList.add(new Test("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));
testList.add(new Test("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));
testList.add(new Test("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//获取姓名集合
List<String> nameList = testList.stream().map(Test::getName()).collect(Collectors.toList());
System.out.println("value:"+nameList);
}
}
11. List<Object> object to List<Object>
public class Stream_object_object {
public static void main(String[] args) {
List<People> peopleList = new ArrayList<People>();
peopleList.add(new People("张三",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("李四",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("王五",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//对象转对象
List<Student> studentList = peopleList.stream().map(s->{
return new Student(s.getName(),s.getAge());
}).collect(Collectors.toList());
System.out.println("value:"+studentList);
}
}
Summarize
slightly