英文:
how to identify duplicate values in a hashmap
问题
以下是翻译好的内容:
有没有简单的方法来识别 HashMap
中的重复值?
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "a");
map.put(3, "b");
我想将重复的值保存在名为 String duplicate
的变量中。
我希望输出结果为 a
。
谢谢大家的帮助,我会尝试你们的建议。谢谢!
英文:
Is there any simple way to identify duplicate values in a HashMap
?
HashMap<Integer, String> map= new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "a");
map.put(3, "b");
I want to save the duplicate value in a variable named String duplicate
.
I want the output a
.
Thank you all for your help, I'll try your tips. Thanks!
答案1
得分: 2
尝试这个。
Map<Integer, String> map = Map.of(1, "a", 2, "b", 3, "b", 4,
"c", 5, "a", 6, "a", 7, "v");
- 获取所有值的集合。
- 遍历该集合的一个 set,移除遇到的每个值的第一个。这样只会在集合中留下重复的值。
- 然后将它们作为一个显示重复项的 set 打印出来。
Collection<String> all = map.values();
new HashSet<>(all).forEach(all::remove);
System.out.println(new HashSet<>(all));
输出
[a, b]
英文:
Try this.
Map<Integer, String> map = Map.of(1, "a", 2, "b", 3, "b", 4,
"c", 5, "a", 6, "a", 7, "v");
- Obtain the collection of all the values.
- Iterate over a set of that collection, removing the first of each value encountered. This leaves only the duplicates in the collection.
- Then print them as a set showing the duplicates.
Collection<String> all = map.values();
new HashSet<>(all).forEach(all::remove);
System.out.println(new HashSet<>(all));
Prints
[a, b]
</details>
# 答案2
**得分**: 0
为了从哈希映射中获取值,您需要对其进行迭代。然后,您可以将它们简单地放入字符串的哈希集中。如果在哈希集中发现任何已经存在的值,它是重复的。
<details>
<summary>英文:</summary>
In order to get values in Hashmap, you are required to iterate across it. Then you can simply put them in HashSet of String. If you find any value already in HashSet, it is repeated.
</details>
# 答案3
**得分**: 0
使用[流 API][1],您可以执行类似以下操作:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
// 在此处填充地图
....
// 打印所有出现超过1次的值
System.out.println(map.values()
.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet()));
输出:
[a]
英文:
Using stream API, you can do something like
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
//populate map here
....
//print all values that occur more then 1 time
System.out.println(map.values()
.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet()));
Output
[a]
答案4
得分: -1
你可以遍历映射的值(首先将它们放入一个列表中),然后通过检查列表中出现多次的元素(通过检查特定元素的第一个和最后一个索引是否不同)来查找。
如果它们不同,意味着该值出现了多次。
以下是使用Java Stream的代码。
List<String> values = new ArrayList<>(map.values());
Set<String> duplicates = values.stream().filter(item -> values.indexOf(item) != values.lastIndexOf(item)).collect(Collectors.toSet());
英文:
You can iterate over the map values (by first taking them in a list)
and look for elements in the list that are occurring more then once by checking if the first and last index particular element is not the same.
If its not same, it means that value is present more than once.
Below is the code using Java Stream.
List<String> values = new ArrayList<>(map.values());
Set<String> duplicates = values.stream().filter(item -> values.indexOf(item) != values.lastIndexOf(item)).collect(Collectors.toSet());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论