General usage of new features of java8

1. Create entity class Stu

@Builder
public class Stu {
    @NotNull
    private Long id;
    @NotNull
    private String name;
    @NotNull
    private String sex;
    @NotNull
    private Integer myAge;

    public Stu() {

    }

    public Stu(Long id, String name, String sex, Integer myAge) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.myAge = myAge;
    }

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getMyAge() {
        return myAge;
    }

    public void setMyAge(Integer myAge) {
        this.myAge = myAge;
    }
}

2. Test method

public static void main(String[] args) {
        List<Stu> stuList = new ArrayList<>();
        Stu stu1 = Stu.builder().id(1L).sex("男").name("admin1").myAge(18).build();
        Stu stu2 = Stu.builder().id(2L).sex("男").name("admin2").myAge(19).build();
        Stu stu3 = Stu.builder().id(3L).sex("女").name("admin3").myAge(18).build();
        Stu stu4 = Stu.builder().id(4L).sex("男").name("admin4").myAge(18).build();
        stuList.add(stu1);
        stuList.add(stu2);
        stuList.add(stu3);
        stuList.add(stu4);
        // traverse the list
        stuList.forEach(stu -> System.out.println("stu = [" + stu + "]"));

        // filter data whose age is 18
        List<Stu> ageFilterList = stuList.stream().filter(stu ->
                stu.getMyAge().equals(18)).collect(Collectors.toList());
        System.out.println("Filter data with age 18 = "+JSON.toJSONString(ageFilterList));

        // Count the number of people whose age is 18
        Long ageCount = stuList.stream().filter(stu -> stu.getMyAge()==18).count();
        System.out.println("ageCount = [" + ageCount + "]");

        // group by gender
        Map<String, List<Stu>> groupMap = stuList.stream().collect(Collectors.groupingBy(stu ->
                stu.getSex ()));
        System.out.println("Group by gender = "+JSON.toJSONString(groupMap));

        // List converts to Map
        Map<Long, Object> map = stuList.stream().collect(Collectors.toMap
        (Stu::getId,Stu::getName));
        map.forEach((k, v) -> System.out.println("list转map  k = [" + k + "]"+"v = ["+v+"]"));

        // allMatch judges that the age of all Stu is 18
        boolean matched =  stuList.stream().allMatch(stu -> stu.getMyAge()==18);
        System.out.println("matched = [" + matched + "]");

        // return the list of names
        List<String> names = stuList.stream().map(stu ->
                stu.getName()).collect(Collectors.toList());
        System.out.println("names = [" + JSON.toJSONString(names) + "]");

        //find the first one whose gender is male
        Optional<Stu> optionals = stuList.stream().filter(stu ->
                stu.getSex().equals("男")).findFirst();
        System.out.println("Employee whose gender is male = [" + JSON.toJSONString(optionals) + "]");

        // find the total years of age
        Long myageCount = stuList.stream().mapToLong(Stu::getMyAge).sum();
        System.out.println("Total Age = [" + myageCount + "]");

        // List information in descending order of age
        List<Stu> sortAgeStuList = stuList.stream().sorted((e1,e2)->
                Integer.compare(e2.getMyAge(),e1.getMyAge())).collect(Collectors.toList());
        System.out.println("Age in descending order = [" + JSON.toJSONString(sortAgeStuList) + "]");

        // First two items in descending order of age
        List<Stu> sortAgeLimit = stuList.stream().sorted((e1,e2)->
                Integer.compare(e2.getMyAge(),e1.getMyAge())).limit(2).collect(Collectors.toList());
        System.out.println("Age in descending order = [" + JSON.toJSONString(sortAgeLimit) + "]");

        // get the average age
        OptionalDouble avgAge = stuList.stream().mapToInt(c->c.getMyAge()).average();
        System.out.println("Average Age = "+avgAge.getAsDouble());

        // Check if all ages are 19 and return a boolean array
        List<Boolean> list = stuList.stream().map(stu -> stu.getMyAge().intValue() == 19).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(list));

        // replace all age values
        List<Stu> stuListReplace = stuList.stream().map((stu) -> {
                    stu.setMyAge (5);
                    return stu;
                }).collect(Collectors.toList());
        System.out.println("Replaced collection = [" + JSON.toJSONString(stuListReplace) + "]");
    }

}
Console prints:
stu = [Stu{id=1, name='admin1', sex='男', myAge=18}]
stu = [Stu{id=2, name='admin2', sex='男', myAge=19}]
stu = [Stu{id=3, name='admin3', sex='女', myAge=18}]
stu = [Stu{id=4, name='admin4', sex='男', myAge=18}]
过滤年龄为18的数据 = [{"id":1,"myAge":18,"name":"admin1","sex":"男"},{"id":3,"myAge":18,"name":"admin3","sex":"女"},{"id":4,"myAge":18,"name":"admin4","sex":"男"}]
ageCount = [3]
按照性别分组 = {"女":[{"id":3,"myAge":18,"name":"admin3","sex":"女"}],"男":[{"id":1,"myAge":18,"name":"admin1","sex":"男"},{"id":2,"myAge":19,"name":"admin2","sex":"男"},{"id":4,"myAge":18,"name":"admin4","sex":"男"}]}
list转map  k = [1]v = [admin1]
list转map  k = [2]v = [admin2]
list转map  k = [3]v = [admin3]
list转map  k = [4]v = [admin4]
matched = [false]
names = [["admin1","admin2","admin3","admin4"]]
Employee whose gender is male = [{"id":1,"myAge":18,"name":"admin1","sex":"male"}]
Total Age = [73]
年龄的降序 = [[{"id":2,"myAge":19,"name":"admin2","sex":"男"},{"id":1,"myAge":18,"name":"admin1","sex":"男"},{"id":3,"myAge":18,"name":"admin3","sex":"女"},{"id":4,"myAge":18,"name":"admin4","sex":"男"}]]
年龄的降序 = [[{"id":2,"myAge":19,"name":"admin2","sex":"男"},{"id":1,"myAge":18,"name":"admin1","sex":"男"}]]
Average age = 18.25
[false,true,false,false]
替换后的集合 = [[{"id":1,"myAge":5,"name":"admin1","sex":"男"},{"id":2,"myAge":5,"name":"admin2","sex":"男"},{"id":3,"myAge":5,"name":"admin3","sex":"女"},{"id":4,"myAge":5,"name":"admin4","sex":"男"}]]



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325771650&siteId=291194637