java 8 StreamTest

忘了在哪看的,挺好用


import lombok.Data;

/**

* Description:    流測試類

* Author:         WangZW

* CreateDate:     2018/12/24

*/

@Data

public class TestStreamModel {

    private int id;

    private String name;

    private int grade;

    private int classes;

    private double score;

}


import com.alibaba.acm.shaded.com.google.common.collect.Lists

import java.util.Comparator

import java.util.List;

import java.util.Map;

import java.util.stream.Collectors;

/**

* Description:    TODO

* Author:         WangZW
*/

public class StreamTest {

    private List<TestStreamModel> getList() {

        List<TestStreamModel> list = Lists.newArrayList();

        TestStreamModel testStreamModel = new TestStreamModel();

        testStreamModel.setId(2);/*主鍵*/

        testStreamModel.setName("張三");/*姓名*/

        testStreamModel.setClasses(1);/*班級*/

        testStreamModel.setGrade(1);/*年級*/

        testStreamModel.setScore(80);/*成績*/

        list.add(testStreamModel);

        TestStreamModel testStreamModel1 = new TestStreamModel();

        testStreamModel1.setId(1);

        testStreamModel1.setName("李四");

        testStreamModel1.setClasses(1);

        testStreamModel1.setGrade(1);

        testStreamModel1.setScore(60);

        list.add(testStreamModel1);

        TestStreamModel testStreamModel2 = new TestStreamModel();

        testStreamModel2.setId(3);

        testStreamModel2.setName("王二麻子");

        testStreamModel2.setClasses(2);

        testStreamModel2.setGrade(1);

        testStreamModel2.setScore(90);

        list.add(testStreamModel2);

        TestStreamModel testStreamModel3 = new TestStreamModel();

        testStreamModel3.setId(4);

        testStreamModel3.setName("王五");

        testStreamModel3.setClasses(2);

        testStreamModel3.setGrade(1);

        testStreamModel3.setScore(59.5);

        list.add(testStreamModel3);

        TestStreamModel testStreamModel4 = new TestStreamModel();

        testStreamModel4.setId(8);

        testStreamModel4.setName("小明");

        testStreamModel4.setClasses(1);

        testStreamModel4.setGrade(2);

        testStreamModel4.setScore(79.5);

        list.add(testStreamModel4);

        TestStreamModel testStreamModel5 = new TestStreamModel();

        testStreamModel5.setId(5);

        testStreamModel5.setName("小紅");

        testStreamModel5.setClasses(2);

        testStreamModel5.setGrade(2);

        testStreamModel5.setScore(99);

        list.add(testStreamModel5);

        TestStreamModel testStreamModel6 = new TestStreamModel();

        testStreamModel6.setId(7);

        testStreamModel6.setName("小黑");

        testStreamModel6.setClasses(2);

        testStreamModel6.setGrade(2);

        testStreamModel6.setScore(45);

        list.add(testStreamModel6);

        TestStreamModel testStreamModel7 = new TestStreamModel();

        testStreamModel7.setId(6);

        testStreamModel7.setName("小白");

        testStreamModel7.setClasses(1);

        testStreamModel7.setGrade(2);

        testStreamModel7.setScore(88.8);

        list.add(testStreamModel7);

        TestStreamModel testStreamModel8 = new TestStreamModel();

        testStreamModel8.setId(6);

        testStreamModel8.setName("小白");

        testStreamModel8.setClasses(1);

        testStreamModel8.setGrade(2);

        testStreamModel8.setScore(88.8);

        list.add(testStreamModel8);

        return list;

    }



    public static void main(String[] args) {

        StreamTest streamTest = new StreamTest();

        List<TestStreamModel> list = streamTest.getList();

        /*去重,去除重複對象(每個屬性的值都一樣的),需要注意的是要先重寫對象TestStreamModel的equals和hashCode方法*/

        System.out.println("去重前:" + list);

        List<TestStreamModel> distinctList = list.stream().distinct().collect(Collectors.toList());

        System.out.println("去重後:" + distinctList);

        /*排序,按id升續排列,如果要降續則改成:(a, b) -> b.getId() - a.getId(); a和b都是變量名(可以按自己意願取名字),都是list中的對象的實例*/

//        下面代碼以自然序排序一個list

//        list.stream().sorted();

//        自然序逆序元素,使用Comparator 提供的reverseOrder() 方法

//        list.stream().sorted(Comparator.reverseOrder());

//        例如

//        使用Comparator 來排序一個list

        list.stream().sorted(Comparator.comparing(TestStreamModel::getId));

//        把上面的元素逆序

        list.stream().sorted(Comparator.comparing(TestStreamModel::getId).reversed());

        System.out.println("排序前:" + list);

        List<TestStreamModel> sortList = list.stream().sorted((a, b) -> a.getId() - b.getId()).collect(Collectors.toList());

        List<TestStreamModel> sortList2 = list.stream().sorted(Comparator.comparing(TestStreamModel::getId).reversed()).collect(Collectors.toList());

        System.out.println("排序後" + sortList);

        System.out.println("排序後" + sortList2);

        /*過濾,按照自己的需求來篩選list中的數據,比如我篩選出不及格的(小於60分)的人,t爲實例*/

        System.out.println("過濾後:" + list);

        List<TestStreamModel> filterList = list.stream().filter(t -> t.getScore() < 60).collect(Collectors.toList());

        System.out.println("過濾後" + filterList);

        /*map, 提取對象中的某一元素,例子中我取的是每個人的name,注意list中類型對應,如果取的是id或者班級,就應該是integer類型*/

        System.out.println("提取前:" + list);

        List<String> mapList = list.stream().map(t -> t.getName()).collect(Collectors.toList());

        //另一種寫法

        List<String> mapList2 = list.stream().map(TestStreamModel::getName).collect(Collectors.toList());

        System.out.println("提取後:" + mapList);

        /*統計,統計所有人分數的和, 主要我設置的分數屬性是double類型的,所以用mapToDouble,如果是int類型的,則需要用mapToInt*/

        double sum = list.stream().mapToDouble(t -> t.getScore()).sum();

        double sum2 = list.stream().mapToDouble(TestStreamModel::getScore).sum();

        int count = list.stream().mapToInt(t -> t.getId()).sum();

        /*分組, 按照字段中某個屬性將list分組*/

        Map<Integer, List<TestStreamModel>> map = list.stream().collect(Collectors.groupingBy(t -> t.getGrade()));

        System.out.println("按年級分組" + map);

        /*然後再對map處理,這樣就方便取出自己要的數據*/

        for (Map.Entry<Integer, List<TestStreamModel>> entry : map.entrySet()) {

            System.out.println("key:" + entry.getKey());

            System.out.println("value:" + entry.getValue());

        }

        /*多重分組,先按年級分組,再按班級分組*/

        Map<Integer/*年級id*/, Map<Integer/*班級id*/, List<TestStreamModel>>> groupMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses())));

        System.out.println("按照年級再按班級分組:" + groupMap);

        System.out.println("取出一年級一班的list:" + groupMap.get(1).get(1));

        /*多重分組,一般多重分組後都是爲了統計,比如說統計每個年級,每個班的總分數*/

        Map<Integer/*年級id*/, Map<Integer/*班級id*/, Double>> sumMap = list.stream().collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.summingDouble(t -> t.getScore()))));

        System.out.println(sumMap);

        System.out.println("取出一年級一班的總分:" + sumMap.get(1).get(1));

        /*stream是鏈式的,這些功能是可以一起使用的,例如:計算每個年級每個班的及格人數*/

        Map<Integer/*年級*/, Map<Integer/*班級*/, Long/*人數*/>> integerMap = list.stream().filter(t -> t.getScore() >= 60).collect(Collectors.groupingBy(t -> t.getGrade(), Collectors.groupingBy(t -> t.getClasses(), Collectors.counting())));

        System.out.println("取出一年級一班及格人數:" + integerMap.get(1).get(1));

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章