list通过lambda 表达式去重,筛选
List<User> distinctList = new ArrayList();
User user1 = new User();
user1.setId("111");
distinctList.add(user1);
User user2 = new User();
user2.setId("222");
distinctList.add(user2);
User user3 = new User();
user3.setId("333");
distinctList.add(user3);
User user4 = new User();
user4.setId("333");
distinctList.add(user4);
List<User> res = distinctList.stream().collect(//list是需要去重的list,返回值是去重后的list
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new));
//筛选
res = distinctList.stream().filter(ls->ls.getId().equals("222")).collect(Collectors.toList());
System.out.println(res);
获取最大最小值
public class GetMax {
public static void main(String[] args) {
List<Integer> num = new ArrayList<Integer>();
num .add(1);
num .add(2);
num .add(3);
num .add(5);
num .add(6);
num .add(15);
num .add(2);
num .add(5);
num .add(6);
num .add(8);
Integer max = num.stream().reduce(Integer::max).get();//得到最大值
Integer min = num.stream().reduce(Integer::min).get();//得到最小值
System.out.println(max);
System.out.println(min);
Integer sum = num.stream().reduce(0,(a, b) -> a + b);
System.out.println(sum);
}