Java Lambda表达式总结

实体 

public class Info {


	/**
	 * 编号
	 */
	private Integer id;


	/**
	 * 姓名
	 */
	private String name;


	/**
	 * 年龄
	 */
	private Integer age;


	/**
	 * 性别
	 */
	private String sex;


	/**
	 * 分数
	 */
	private BigDecimal score;


	/**
	 * 地址
	 */
	private String address;


	public Integer getId() {
		return id;
	}

	public void setId(Integer 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;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public BigDecimal getScore() {
		return score;
	}

	public void setScore(BigDecimal score) {
		this.score = score;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}


	public Info() {
	}

	public Info(Integer id, String name, Integer age, String sex, BigDecimal score, String address) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.score = score;
		this.address = address;
	}
}

1、分组

//分组
Map<String, List<Info>> groupBySex = infoList.stream().collect(Collectors.groupingBy(Info::getSex));
//遍历分组
for (Map.Entry<String, List<Info>> entryInfo : groupBySex.entrySet()) {
    String key = entryInfo.getKey();
    List<Info> entryInfoList = entryInfo.getValue();
}

2、求和

//基本类型
int sumAge = infoList.stream().mapToInt(Info::getAge).sum();

//BigDecimal求和
BigDecimal totalScore = infoList.stream().map(Info::getScore).reduce(BigDecimal.ZERO, BigDecimal::add);

3、排序

//单字段排序,根据年龄排序
infoList.sort(Comparator.comparing(Info::getAge));
//多字段排序,根据年龄,分数排序
infoList.sort(Comparator.comparing(Info::getAge).thenComparing(Info::getScore));

4、过滤

//获取年龄大于20岁的信息
List<Info> collectList = infoList.stream().filter(t -> t.getAge() > 20).collect(Collectors.toList());

5、List转Map

 /**
   * List -> Map
   * 需要注意的是:
   * toMap 如果集合对象有重复的key,会报错Duplicate key ....
   *  user1,user2的id都为1。
   *  可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
   */
   Map<Integer, Info> collectMap = infoList.stream().collect(Collectors.toMap(Info::getId, Function.identity(), (k1, k2) -> k1));

6、最值

//最小(也可获取日期)
Integer minAge = infoList.stream().map(Info::getAge).min(Integer::compareTo).get();

//最大
Integer maxAge = infoList.stream().map(Info::getAge).max(Integer::compareTo).get();

7、List取交集/并集/差集/去重并集

List<String> list1 = Lists.newArrayList();
list1.add("1111");
list1.add("2222");
list1.add("3333");

List<String> list2 = Lists.newArrayList();
list2.add("3333");
list2.add("4444");
list2.add("5555");

// 交集 	拓展:list2里面如果是对象,则需要提取每个对象的某一属性组成新的list,多个条件则为多个list
List<String> intersection = list1.stream().filter(list2::contains).collect(Collectors.toList());
System.out.println("---得到交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);

// 差集 (list1 - list2)	同上拓展
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);

// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);

// 并集
List<String> listAll = list1.parallelStream().collect(Collectors.toList());
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out :: println);

// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEach(System.out :: println);

System.out.println("---原来的List1---");
list1.parallelStream().forEach(System.out :: println);
System.out.println("---原来的List2---");
list2.parallelStream().forEach(System.out :: println);

// 一般有filter 操作时,不用并行流parallelStream ,如果用的话可能会导致线程安全问题

8、获取List某个字段重新组装成一个新的List

List<String> nameList = infoList.stream().map(a -> a.getName()).collect(Collectors.toList());

9、去重

List<Integer> list = Lists.newArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
List<Integer> distinctList = list.stream().distinct().collect(Collectors.toList());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值