栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Lambada Java具体用法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Lambada Java具体用法

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class Lambada {

    public static void main(String[] args) {

        List appleList = new ArrayList<>();//存放apple对象集合

        Apple apple1 = new Apple(1, "苹果1", new BigDecimal("3.25"), 10);
        Apple apple12 = new Apple(1, "苹果2", new BigDecimal("1.35"), 20);
        Apple apple2 = new Apple(2, "香蕉", new BigDecimal("2.89"), 30);
        Apple apple3 = new Apple(3, "荔枝", new BigDecimal("9.99"), 40);

        appleList.add(apple1);
        appleList.add(apple12);
        appleList.add(apple2);
        appleList.add(apple3);

        // 获取Apple的价格属性
        List collect = appleList.stream().map(Apple::getMoney).collect(Collectors.toList());
        System.out.println(collect);

        System.out.println("=========================================================================================");

        // List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起
        // List 以ID分组 Map
        Map> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
        groupBy.forEach((k, v) -> {
            System.out.println(k + ": " + v);
        });

        System.out.println("=========================================================================================");

        // List转Map,id为key,apple对象为value
        
        Map appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a, (k1, k2) -> k1));
        appleMap.forEach((k, v) -> {
            System.out.println(k + ": " + v);
        });

        System.out.println("=========================================================================================");

        // 从集合中过滤出来符合条件的元素
        List filterList = appleList.stream().filter(a -> a.getName().equals("香蕉")).collect(Collectors.toList());
        filterList.forEach(System.out::println);

        System.out.println("=========================================================================================");

        // 将集合中的数据按照某个属性求和
        BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("totalMoney:"+totalMoney);  //totalMoney:17.48

        System.out.println("=========================================================================================");

		// 最大值 最小值
        Optional max = appleList.stream().collect(Collectors.maxBy(Comparator.comparing(Apple::getMoney)));
        max.ifPresent(System.out::println);

        Optional min = appleList.stream().collect(Collectors.minBy(Comparator.comparing(Apple::getMoney)));
        min.ifPresent(System.out::println);
        System.out.println(min.get());

        System.out.println("=========================================================================================");

        // 根据id去重
        List unique = appleList.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparingLong(Apple::getId))),
                        ArrayList::new
                )
        );
        unique.forEach(System.out::println);
    }

}

Apple类:

import lombok.Data;
import java.math.BigDecimal;

@Data
public class Apple {
    private Integer id;
    private String name;
    private BigDecimal money;
    private Integer num;
    public Apple(Integer id, String name, BigDecimal money, Integer num) {
        this.id = id;
        this.name = name;
        this.money = money;
        this.num = num;
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930558.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号