Java8 - Collections.toMap()方法

Collections.toMap()方法使用
1)实体类:

@ToString
@Data
@AllArgsConstructor
public class People {

    private Integer id;
    private String name;
    private String address;
    private Integer age;
}

2)测试类

 public static void main(String[] args) {
        People people1 = new People(1, "only-qi1", "bbb", 11);
        People people2 = new People(2, "only-qi2", "ccc", 12);
        People people3 = new People(3, "only-qi3", "dddd", 13);
        People people4 = new People(1, "only-qi4", "fff", 14);
        People people5 = new People(2, "only-qi5", "eee", 15);
        People people6 = new People(1, "only-qi6", "ttttt", 11);
        ArrayList<People> arrayList = new ArrayList<>();
        arrayList.add(people1);
        arrayList.add(people2);
        arrayList.add(people3);
        arrayList.add(people4);
        arrayList.add(people5);
        arrayList.add(people6);

        System.out.println("-----------------toMap()方法: 收集一下集合中每个对象的两个单独的属性---------------");
        Map<String, String> collect = arrayList.stream().collect(Collectors.toMap(People::getName, People::getAddress));
        for (String s : collect.keySet()) {
            System.out.println("key值是:" + s + "=========" + "value的值是" + collect.get(s));
        }

        arrayList.stream().collect(Collectors.toMap(People::getName, p -> p));

        System.out.println("-------------------toMap()方法:收集一下属性和对象本身----------------------");
        Map<String, People> collect1 = arrayList.stream().collect(Collectors.toMap(People::getName, p -> p));
        for (String s : collect1.keySet()) {
            System.out.println("key值是:" + s + "=========" + "value的值是" + collect1.get(s));
        }
    }

注:
使用toMap()函数之后,返回的就是一个Map了,自然会需要key和value。
toMap()的第一个参数就是用来生成key值的,第二个参数就是用来生成value值的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 8中的Collectors.toMap方法可以将Stream中的元素转换为一个Map对象。该方法有三个参数:keyMapper,valueMapper和mergeFunction。其中,keyMapper用于将Stream中的元素转换为Map的key,valueMapper用于将Stream中的元素转换为Map的value,mergeFunction用于处理当key重复时的冲突情况。 下面是一个例子,假设我们有一个List对象,其中包含多个Person对象,每个Person对象都有一个唯一的id属性和一个name属性。我们可以使用Collectors.toMap方法将List转换为一个以id为key,以Person对象为value的Map对象: ```java List<Person> personList = new ArrayList<>(); // 假设我们已经将多个Person对象添加到了personList中 Map<Integer, Person> personMap = personList.stream() .collect(Collectors.toMap(Person::getId, Function.identity())); ``` 在上面的例子中,Person::getId表示将Person对象的id属性作为Map的key,Function.identity()表示将Person对象本身作为Map的value。如果我们想要将Person对象的name属性作为Map的value,可以这样写: ```java Map<Integer, String> personMap = personList.stream() .collect(Collectors.toMap(Person::getId, Person::getName)); ``` 如果我们的List中有重复的id,那么上面的代码将会抛出IllegalStateException异常。为了避免这种情况,我们可以使用mergeFunction参数来处理冲突。例如,如果我们想要将重复的id的Person对象合并为一个List,可以这样写: ```java Map<Integer, List<Person>> personMap = personList.stream() .collect(Collectors.toMap(Person::getId, Collections::singletonList, (list1, list2) -> { List<Person> resultList = new ArrayList<>(list1); resultList.addAll(list2); return resultList; })); ``` 在上面的代码中,Collections::singletonList表示将Person对象转换为只包含一个元素的List,mergeFunction参数则表示将两个List合并为一个List。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值