Java8Lambda表达式进行list集合排序和list集合转map,过滤,字符串拼接,遍历...

2023-04-08 13:25:36

java8中Lambda表达式简单使用:
java8正序、倒序排序、list集合转map<String,List>

实体类:

@Data
class Apple{

    private String id; //苹果id

    private String productDate; //生产日期

    private BigDecimal price; //价格
}

测试:

   List<Apple> appleList = new ArrayList<>();

        Apple apple1 = new Apple();
        apple1.setId("1");
        apple1.setProductDate("2020-04-12");
        apple1.setPrice(new BigDecimal(5500));

        Apple apple2 = new Apple();
        apple2.setId("2");
        apple2.setProductDate("2020-04-10");
        apple2.setPrice(new BigDecimal(5000));

        Apple apple3 = new Apple();
        apple3.setId("3");
        apple3.setProductDate("2020-04-11");
        apple3.setPrice(new BigDecimal(6500));

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

1.正序:

appleList = appleList.stream().sorted(Comparator.comparing(Apple::getProductDate)).collect(Collectors.toList());
System.out.println(appleList); //打印

运行结果:
[Apple(id=2, productDate=2020-04-10, price=5000), Apple(id=3, productDate=2020-04-11, price=6500), Apple(id=1, productDate=2020-04-12, price=5500)]

2.倒序:

 appleList = appleList.stream().sorted(Comparator.comparing(Apple::getProductDate).reversed()).collect(Collectors.toList());
System.out.println(appleList); //打印

运行结果:
 [Apple(id=1, productDate=2020-04-12, price=5500), Apple(id=3, productDate=2020-04-11, price=6500), Apple(id=2, productDate=2020-04-10, price=5000)]

3.根据某个字段list集合转map
这里我进行list转map,去掉重复的key值,value为list

 Apple apple4 = new Apple();
        apple4.setId("4");
        apple4.setProductDate("2020-04-12");
        apple4.setPrice(new BigDecimal(7500));
        appleList.add(apple4);
 Map<String, List<Apple>> map = appleList.stream().
                collect(Collectors.groupingBy(Apple::getProductDate));
System.out.println(map);

运行结果:
{2020-04-12=[Apple(id=1, productDate=2020-04-12, price=5500), Apple(id=4, productDate=2020-04-12, price=7500)], 2020-04-11=[Apple(id=3, productDate=2020-04-11, price=6500)], 2020-04-10=[Apple(id=2, productDate=2020-04-10, price=5000)]}
       

4.过滤
过滤出productDate和price条件

appleList = appleList.parallelStream().filter(apple -> apple.getProductDate()=="2020-04-12" && 7500 == apple.getPrice().intValue()).collect(Collectors.toList());
System.out.println(appleList);
运行结果:
[Apple(id=4, productDate=2020-04-12, price=7500)]

5.limit

appleList = appleList.parallelStream().limit(1).filter(apple -> apple.getProductDate()=="2020-04-12" ).collect(Collectors.toList());
System.out.println(appleList);

运行结果:
[Apple(id=1, productDate=2020-04-12, price=5500)]

6.遍历


System.out.println(appleList); //打印
运行结果:
[Apple(id=1, productDate=2020-04-12, price=5500), Apple(id=2, productDate=2020-04-10, price=5000), Apple(id=3, productDate=2020-04-11, price=6500), Apple(id=4, productDate=2020-04-12, price=7500)]

appleList.stream().forEach(apple -> {
            System.out.println(apple);
        });
运行结果:
Apple(id=1, productDate=2020-04-12, price=5500)
Apple(id=2, productDate=2020-04-10, price=5000)
Apple(id=3, productDate=2020-04-11, price=6500)
Apple(id=4, productDate=2020-04-12, price=7500)

7.字符串过滤和拼接

List<String> strings = Arrays.asList("abc", "", "ab", "def", "gh","i", "efg");
strings = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
System.out.println(strings);
运行结果:
[abc, ab, def, gh, i, efg]
  
  //拼接
String splicing = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining("-"));
System.out.println(splicing);
运行结果:
abc-ab-def-gh-i-efg
  • 作者:K星人
  • 原文链接:https://blog.csdn.net/m0_46436491/article/details/105413005
    更新时间:2023-04-08 13:25:36