Java 8 Lambda式list集合ソートとlist集合回転map,フィルタリング,文字列接合,遍歴...

3995 ワード

JAva 8のLambda式は簡単に使用できます:java 8の正の順序、逆の順序、listの集合はmapを回転します
エンティティークラス:
@Data
class Apple{

    private String id; // id

    private String productDate; // 

    private BigDecimal price; // 
}


テスト:
   List 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> 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 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