需要引包:
compile group: 'com.google.guava', name: 'guava', version: '28.1-jre'
package com.zhuzhuxia.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Author: 猪猪侠
* @Date: 2019/10/30 17:59
* @Version: 1.0
* @Description:
*/
public class MyTest {
@Test
public void listTest(){
List integerList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 3, 5, 1, 4, 2, 8, 9);
List studentList = Lists.newArrayList();
Student stu1 = new Student(1L,"张三",15);
Student stu2 = new Student(2L,"李四",11);
Student stu3 = new Student(3L,"王五",12);
Student stu4 = new Student(4L,"赵六",10);
studentList.add(stu1);
studentList.add(stu2);
studentList.add(stu3);
studentList.add(stu4);
System.out.println("学生原数据:"+JSONArray.toJSON(studentList));
System.out.println("-----------------------------------------------------");
List collect1 = studentList.stream().filter(stu -> stu.getName().equals("张三")).collect(Collectors.toList());
System.out.println("查出姓名是“张三”的学生:"+JSONArray.toJSON(collect1));
List collect2 = studentList.stream().map(stu -> stu.getAge()).collect(Collectors.toList());
System.out.println("取出学生的年龄集合:"+collect2);
List collect6 = studentList.stream().map(stu -> stu.getAge()).distinct().collect(Collectors.toList());
System.out.println("取出学生的年龄集合并去重:"+collect6);
List collect3 = studentList.stream().skip(3).collect(Collectors.toList());
System.out.println("学生集合里去除前三个:"+JSONArray.toJSON(collect3));
List collect4 = studentList.stream().limit(3).collect(Collectors.toList());
System.out.println("学生集合里取前三个:"+JSONArray.toJSON(collect4));
Map> collect5 = studentList.stream().collect(Collectors.groupingBy(student -> student.getAge()));
System.out.println("根据学生年龄分组:"+JSONArray.toJSON(collect5));
int ageSum = studentList.stream().mapToInt(stu -> stu.getAge()).sum();
System.out.println("找出学生年龄之和:"+ageSum);
int ageMax= studentList.stream().mapToInt(stu -> stu.getAge()).max().getAsInt();
System.out.println("找出学生年龄最大:"+ageMax);
int ageMin= studentList.stream().mapToInt(stu -> stu.getAge()).min().getAsInt();
System.out.println("找出学生年龄最小:"+ageMin);
Double aggAverage = studentList.stream().mapToInt(stu -> stu.getAge()).average().getAsDouble();
System.out.println("找出学生年龄最小:"+aggAverage);
studentList.sort((a,b) -> b.getAge().compareTo(a.getAge()));
System.out.println("根据学生年龄降序:"+JSONArray.toJSON(studentList));
studentList.sort((a,b) -> a.getAge().compareTo(b.getAge()));
System.out.println("根据学生年龄升序:"+JSONArray.toJSON(studentList));
}
public static class Student{
private Long id;
private String name;
private Integer age;
public Student(Long id, String name,Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
运行结果:
学生原数据:[{"name":"张三","id":1,"age":15},{"name":"李四","id":2,"age":11},{"name":"王五","id":3,"age":12},{"name":"赵六","id":4,"age":10}]
-----------------------------------------------------
查出姓名是“张三”的学生:[{"name":"张三","id":1,"age":15}]
取出学生的年龄集合:[15, 11, 12, 10]
取出学生的年龄集合并去重:[15, 11, 12, 10]
学生集合里去除前三个:[{"name":"赵六","id":4,"age":10}]
学生集合里取前三个:[{"name":"张三","id":1,"age":15},{"name":"李四","id":2,"age":11},{"name":"王五","id":3,"age":12}]
根据学生年龄分组:{"11":[{"name":"李四","id":2,"age":11}],"12":[{"name":"王五","id":3,"age":12}],"15":[{"name":"张三","id":1,"age":15}],"10":[{"name":"赵六","id":4,"age":10}]}
找出学生年龄之和:48
找出学生年龄最大:15
找出学生年龄最小:10
找出学生年龄最小:12.0
根据学生年龄降序:[{"name":"张三","id":1,"age":15},{"name":"王五","id":3,"age":12},{"name":"李四","id":2,"age":11},{"name":"赵六","id":4,"age":10}]
根据学生年龄升序:[{"name":"赵六","id":4,"age":10},{"name":"李四","id":2,"age":11},{"name":"王五","id":3,"age":12},{"name":"张三","id":1,"age":15}]