当在期间找到重复的键条目时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)
在合并函数内部,我想根据键来决定,如果我取消映射,或继续并接受这些值.