相关疑难解决方法(0)

如何在Collectors.toMap合并函数中获取密钥?

当在期间找到重复的键条目时Collectors.toMap(),(o1, o2)调用合并功能.

问题:如何获取导致重复的密钥?

String keyvalp = "test=one\ntest2=two\ntest2=three";

Pattern.compile("\n")
    .splitAsStream(keyval)
    .map(entry -> entry.split("="))
    .collect(Collectors.toMap(
        split -> split[0],
        split -> split[1],
        (o1, o2) -> {
            //TODO how to access the key that caused the duplicate? o1 and o2 are the values only
            //split[0]; //which is the key, cannot be accessed here
        },
    HashMap::new));
Run Code Online (Sandbox Code Playgroud)

在合并函数内部,我想根据来决定,如果我取消映射,或继续并接受这些值.

java java-8 collectors

12
推荐指数
2
解决办法
3765
查看次数

标签 统计

collectors ×1

java ×1

java-8 ×1