package com.example.demo2;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
public class LambdaHelper2 {
public static void main(String[] args) {
List list = Lists.newArrayList();
User user = new User();
user.setAge(10);
user.setId(1L);
user.setFamilyMemberQuantity(BigDecimal.valueOf(2));
list.add(user);
user = new User();
user.setId(2L);
user.setAge(15);
user.setFamilyMemberQuantity(BigDecimal.valueOf(3));
list.add(user);
user = new User();
user.setId(2L);
user.setAge(15);
//user.setFamilyMemberQuantity(BigDecimal.valueOf(3));
list.add(user);
test3(list);
}
//按性别字段分组
private static void test1(List list) {
//按性别字段分组
Map> map = list.stream().collect(Collectors.groupingBy(x->x.getAge()));
// or 其中通过类名+方法名引用时 方法不需要带括号
Map> map2 = list.stream().collect(Collectors.groupingBy(User::getAge));
}
//过滤
private static void test2(List list) {
Listfilters = list.stream().filter(x->x.getJobNumber().equals("20010101")).collect(Collectors.toList());
}
//求和
private static void test3(List list) {
//int 求和
int sumage = list.stream().mapToInt(User::getAge).sum();
//bigdecimal求和
//BigDecimal sum = list.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal sum = list.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimalUtils::sum);
System.out.println("sumage:"+sumage+" sum:"+sum);
}
//判断非空
private static void test4(List list) {
//list 非空
Objects.requireNonNull(list);
//对象非空
List collect = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
//字段判空
List collect2 = list.stream().filter(x->x.getFamilyMemberQuantity()!=null).collect(Collectors.toList());
//字段判空
List collect3 = list.stream().filter(x->Objects.nonNull(x.getFamilyMemberQuantity())).collect(Collectors.toList());
}
//最值
private static void test5(List list) {
Objects.requireNonNull(list);
Integer integer = list.stream().map(User::getAge).min(Integer::compareTo).get();
Date date = list.stream().map(User::getEntryDate).max(Date::compareTo).get();
}
/**
* list 转 map
* * List -> Map
3 * 需要注意的是:
4 * toMap 如果集合对象有重复的key,会报错Duplicate key ....
5 * user1,user2的id都为1。
6 * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
*/
private static void test6(List list) {
Map userMap = list.stream().collect(Collectors.toMap(User::getId, a->a,(k1,k2)->k1));
Map userMap2 = list.stream().collect(Collectors.toMap(User::getId, a->a,(k1,k2)->k1));
list.stream().collect(Collectors.toMap(User::getId,a->a,(k1,k2)->k1));
}
//排序
private static void test7(List list) {
list.sort(Comparator.comparing(User::getId).reversed());
list.sort(Comparator.comparing(User::getAge).thenComparing(Comparator.comparing(User::getId)));
}
//去重
private static void test8(List list) {
list.stream().distinct().map(a->a.getAge()).collect(Collectors.toList());
}
}
class BigDecimalUtils{
public static BigDecimal ifNullSet0(BigDecimal num){
return null == num? BigDecimal.ZERO:num;
}
public static BigDecimal sum(BigDecimal ... args) {
BigDecimal result = BigDecimal.ZERO;
for(BigDecimal arg:args) {
result=result.add(ifNullSet0(arg));
}
return result;
}
}
class User {
/**
* id
*/
private Long id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 工号
*/
private String jobNumber;
/**
* 性别
*/
private String sex;
/**
* 入职日期
*/
private Date entryDate;
/**
* 家庭成员数量
*/
private BigDecimal familyMemberQuantity;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJobNumber() {
return jobNumber;
}
public void setJobNumber(String jobNumber) {
this.jobNumber = jobNumber;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getEntryDate() {
return entryDate;
}
public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}
public BigDecimal getFamilyMemberQuantity() {
return familyMemberQuantity;
}
public void setFamilyMemberQuantity(BigDecimal familyMemberQuantity) {
this.familyMemberQuantity = familyMemberQuantity;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", jobNumber=" + jobNumber + ", sex=" + sex
+ ", entryDate=" + entryDate + ", familyMemberQuantity=" + familyMemberQuantity + "]";
}
}