如何在哈希映射中识别重复的数值。

huangapple go评论62阅读模式
英文:

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&lt;Integer, String&gt; map= new HashMap&lt;Integer, String&gt;();

map.put(1, &quot;a&quot;);
map.put(2, &quot;a&quot;);
map.put(3, &quot;b&quot;);

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&lt;Integer, String&gt; map = Map.of(1, &quot;a&quot;, 2, &quot;b&quot;, 3, &quot;b&quot;, 4,
				&quot;c&quot;, 5, &quot;a&quot;, 6, &quot;a&quot;, 7, &quot;v&quot;);
  • 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&lt;String&gt; all = map.values();
new HashSet&lt;&gt;(all).forEach(all::remove);

System.out.println(new HashSet&lt;&gt;(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 -&gt; e.getValue() &gt; 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());

huangapple
  • 本文由 发表于 2020年9月13日 19:14:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63870077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定