JAVA8 lambda表达式操作集合

JAVA8 lambda表达式操作集合

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Test {


    public static void main(String[] args) {
        List<User> list = new ArrayList<>(10);

        User user = null;
        for (int i = 0; i < 10; i++) {
            user = new User();
            user.setId("userId" + i);
            user.setUsername("username" + i);
            user.setSex((short) (i % 2));
            user.setAge(i);
            user.setBirthday("19910202");
            if (i % 2 == 0) {
                user.setBirthday("19910303");
            }
            user.setAddress("凤凰路1号");
            System.out.println(JSONObject.toJSONString(user));
            list.add(user);
        }
        System.out.println("***************************************************");

        System.out.println("获取list的某一属性组成List:");
        List<Short> sexList = Lists.transform(list, u -> u.getSex());
        sexList.forEach(i -> {
            System.out.println(i);
        });
        System.out.println("***************************************************");


        System.out.println("将List转成Map<K,V>");
        Map<String, User> objMap = list.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        objMap.forEach((k, v) -> {
            System.out.println("key:" + k + ";value:" + JSONObject.toJSONString(v));
        });
        System.out.println("***************************************************");


        System.out.println("根据某一属性进行分组Map<String,List<Object>>");
        Map<Short, List<User>> groupBy = list.stream().collect(Collectors.groupingBy(User::getSex));
        groupBy.forEach((k, v) -> {
            System.out.println("key:" + k + ";value:" + JSONObject.toJSONString(v));
        });
        System.out.println("***************************************************");

        System.out.println("根据某一属性进行降序排序");
        List<User> sort = list.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
        sort.forEach(u -> System.out.println(JSONObject.toJSONString(u)));
        System.out.println("***************************************************");


    }

    static class User {
        private String id;
        private String username;
        private Short sex;
        private Integer age;
        private String birthday;
        private String address;

        public User() {
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public Short getSex() {
            return sex;
        }

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

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getBirthday() {
            return birthday;
        }

        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this);
        }
    }

}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页