java8

2020-02-10 13:12:48 浏览数 (1)

好久不见,很久没有这么安静的去思考一些内容了,记得很久之前自己说过互联网上还是要谨言慎行的,是的,这是我对自己说的,最新除了在自己朋友圈分享了一点自己的心情,随后就删除了之外,自己也不想把自己的烦心事告诉其它人,避免带来负面消极情绪影响到其它人。

因为自己曾说过,经历皆可用过往来形容了,今天好不容易有时间坐在房间里去写一点自己喜欢的内容,所以接下来的大部分内容文字很少,权当自己给与自己的一点总结而已,我喜欢分享,你喜欢阅读,这就足够了。

代码语言:javascript复制
package com.wpw.learnlearn;



import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.math.BigDecimal;

/**
 * @author pc
 */
@Data
@Accessors(chain = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Apple {
    private Integer id;
    private String name;
    private BigDecimal money;
    private Integer num;
}

首先我们定义一个接下来示例程序需要用到的实体类,好久不写文章,手法都生疏了很多。

说下自己为啥要分享这边文章的原因吧,其实就是自己的一点总结,分享一下,避免去走很多不必要的过程,有的人觉得没必要去分享,自己内心知道就行了,我们尊重每个人的想法,自己喜欢就好,好了,不说其他了,看下下面的示例程序就好了。

代码语言:javascript复制
package com.wpw.learnlearn;

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

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.*;

/**
 * @author pc
 */
public class Test {
    private static final List<Apple> APPLE_LIST = new ArrayList<>();

    static {
        APPLE_LIST.add(new Apple().setId(1).setName("苹果1").setMoney(BigDecimal.valueOf(3.25)).setNum(10));
        APPLE_LIST.add(new Apple().setId(1).setName("苹果2").setMoney(BigDecimal.valueOf(1.35)).setNum(20));
        APPLE_LIST.add(new Apple().setId(2).setName("香蕉").setMoney(BigDecimal.valueOf(2.89)).setNum(30));
        APPLE_LIST.add(new Apple().setId(3).setName("荔枝").setMoney(BigDecimal.valueOf(9.99)).setNum(40));
    }

    public static void main(String[] args) {
        Map<Integer, List<Apple>> collectGroupBy = APPLE_LIST.stream().collect(Collectors.groupingBy(Apple::getId));
        System.out.println("collectGroupBy = "   collectGroupBy);
       
        System.out.println("===========================================");
        Map<Integer, Apple> appleMap = APPLE_LIST.stream().collect(Collectors.toMap(Apple::getId, x -> x, (k1, k2) -> k1));
        System.out.println("appleMap = "   appleMap);
        
        Map<Integer, Apple> map = APPLE_LIST.stream().collect(Collectors.toMap(Apple::getId, y -> y, (k1, k2) -> k2));
        System.out.println("map = "   map);
        
        System.out.println("==========================================");
        
        List<Apple> appleList = APPLE_LIST.stream().filter(x -> "苹果1".equals(x.getName())).collect(Collectors.toList());
        System.out.println("appleList = "   appleList);
        
        Set<Apple> appleSet = APPLE_LIST.stream().filter(x -> "香蕉".equals(x.getName())).collect(Collectors.toSet());
        System.out.println("appleSet = "   appleSet);
        
        List<Apple> list = APPLE_LIST.stream().filter(x -> x.getId() == 1).collect(Collectors.toList());
        list.forEach(System.out::println);
        
        BigDecimal reduce = APPLE_LIST.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("reduce = "   reduce);
        
        Optional<Apple> appleOptional = APPLE_LIST.stream().max(Comparator.comparing(Apple::getMoney));
        appleOptional.ifPresent(System.out::println);
        
        Optional<Apple> appleOptional1 = APPLE_LIST.stream().min(Comparator.comparing(Apple::getMoney));
        appleOptional1.ifPresent(System.out::println);
        
        System.out.println("=====================================================");
        ArrayList<Apple> arrayList = APPLE_LIST.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(
                Apple::getId))), ArrayList::new));
        arrayList.forEach(System.out::println);
        
        System.out.println("===============================");
        Long idCollect = APPLE_LIST.stream().map(Apple::getId).count();
        System.out.println("idCollect = "   idCollect);
        
        int collect = APPLE_LIST.stream().mapToInt(Apple::getId).sum();
        System.out.println("collect = "   collect);
        
        long sum = APPLE_LIST.stream().mapToLong(Apple::getNum).sum();
        System.out.println("sum = "   sum);
        
        String s = APPLE_LIST.stream().map(Apple::getName).collect(joining(","));
        System.out.println("s = "   s);
    }
}

今天要分享的内容就到这里结束了

0 人点赞