package test;

import com.google.common.collect.Lists;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class LambdaTest {

    public static void main(String[] args) {
        List<User> userList = Lists.newArrayList();//存放user对象集合

        LambdaTest.User user1 = new LambdaTest.User(1L, "张三", 24);
        LambdaTest.User user2 = new LambdaTest.User(2L, "李四", 27);
        LambdaTest.User user3 = new LambdaTest.User(3L, "王五", 21);

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

        //ID为key，转为Map
        Map<Long,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1, k2)->k1));
        System.out.println(userMap);

    }

    public static class User{
        private Long id;
        private String name;
        private int age;

        public User(Long id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        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 int getAge() {
            return age;
        }

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