Java Stream操作

POJO類

class FriendInfo {

    private long id;
    private String name;
    private Integer age;
    private String deptId;

    public String getDeptId() { return deptId; }
    public void setDeptId(String deptId) { this.deptId = deptId; }
    public long getId() { return id; }
    public void setId(long 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 FriendInfo(long id, String name, Integer age, String deptId) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.deptId = deptId;
    }
}

一些Stream操作

class Streamtest{
public static void main(String[] args) {


        FriendInfo daxiong = new FriendInfo(123123231, "daxiong", 11, "1");
        FriendInfo wangzai = new FriendInfo(123124122, "wangzai", 16, "1");
        FriendInfo panghu = new FriendInfo(1231234112, "panghu", 10, "2");

        List<FriendInfo> friendInfoList = new ArrayList<>();

        friendInfoList.add(wangzai);
        friendInfoList.add(daxiong);
        friendInfoList.add(panghu);

        //Collect收集器,前一個數(shù)據(jù)獲取,后面還可以跟一個下游收集器,可以進(jìn)行一些處理,下游還能有下游.
        //對比誰的年齡最大,先收集age,下游處理取最大
        Integer c = friendInfoList.stream().collect(Collectors.mapping(FriendInfo::getAge, Collectors.reducing((s1, s2) -> s1 > s2 ? s1 : s2))).get();
        //名字長度累加 ,join也行
        Object s = friendInfoList.stream().collect(Collectors.mapping(FriendInfo::getName , Collectors.toList() )).stream().reduce( (x,y) -> x+y).get().length();
        System.out.println(s);
        //過濾條件:age>10 的留下
        //結(jié)果:wangzai ,daxiong
        friendInfoList.stream().filter(n -> n.getAge() > 10).collect(Collectors.toList()).forEach(n -> System.out.println(n.getName()));
        //類型轉(zhuǎn)換為Int
        // 結(jié)果:16 ,11 ,10
        friendInfoList.stream().mapToInt(FriendInfo::getAge).boxed().collect(Collectors.toList()).forEach(System.out::println);
        //升序排序
        //結(jié)果:panghu ,daxiong, wangzai
        friendInfoList.stream().sorted(Comparator.comparing(FriendInfo::getAge)).collect(Collectors.toList()).forEach(n -> System.out.println(n.getName()));
        //collect()生成Map
        Map<Long, FriendInfo> friendInfoMap = new HashMap<>();
        friendInfoMap = friendInfoList.stream().collect(Collectors.toMap(n -> n.getId(), n -> n));
        //結(jié)果:{1231234112=com.wangzai.servicezuul.FriendInfo@224aed64, 123123231=com.wangzai.servicezuul.FriendInfo@c39f790, 123124122=com.wangzai.servicezuul.FriendInfo@71e7a66b}
        System.out.println(friendInfoMap);
        //Group分組,以deptId為鍵,FriendInfo為值,分組
        //{1=[com.wangzai.servicezuul.FriendInfo@71e7a66b, com.wangzai.servicezuul.FriendInfo@c39f790], 2=[com.wangzai.servicezuul.FriendInfo@224aed64]}
        Map<String, List<FriendInfo>> deptToInfo = friendInfoList.stream().collect(Collectors.groupingBy(FriendInfo::getDeptId));
        System.out.println(deptToInfo);
        //Group分組數(shù)量統(tǒng)計
        Map<String, Long> deptToNumber = friendInfoList.stream().collect(Collectors.groupingBy(FriendInfo::getDeptId, Collectors.counting()));
        System.out.println(deptToNumber);
        //Group分組,deptId為鍵,name為值
        Map<String, List<String>> deptToName = friendInfoList.stream().collect(Collectors.groupingBy(FriendInfo::getDeptId, Collectors.mapping(FriendInfo::getName, Collectors.toList())));
        System.out.println(deptToName);
        //字符串拼接 name拼接起來
        String nameJoin = friendInfoList.stream().collect(Collectors.mapping(FriendInfo::getName, Collectors.joining()));
        System.out.println(nameJoin);

    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,678評論 19 139
  • 本文采用實例驅(qū)動的方式,對JAVA8的stream API進(jìn)行一個深入的介紹。雖然JAVA8中的stream AP...
    浮梁翁閱讀 26,158評論 3 50
  • 轉(zhuǎn)自:IBM-developerworks Stream是什么 看到這個Stream的第一眼,我相信你可能會想到J...
    MentallyL閱讀 1,507評論 0 1
  • 1. Stream初體驗 我們先來看看Java里面是怎么定義Stream的: A sequence of elem...
    kechao8485閱讀 1,282評論 0 9
  • 簡單的個人護(hù)膚史: 本人從初二的時候開始護(hù)膚,雖然那時候護(hù)膚的步驟很粗糙,只是簡單的用洗面奶,然后涂上爽膚水就完事...
    負(fù)薛蘿閱讀 1,032評論 0 4

友情鏈接更多精彩內(nèi)容