使用Java在数组对象中打印所有重复的项目
我想知道如何在一系列对象中打印所有重复的项目。对于Ex:
[
{
"key": "KEY1",
"value": "123",
},
{
"key": "KEY2",
"value": "234",
},
{
"key": "KEY1",
"value": "456"
}
]
代码:我有此逻辑,但它将排除第一次出现Key1,并且仅打印它的重复。
Set<String> duplicates = new HashSet();
..for(i in items)..{
if (duplicates.contains(i.val())) {
System.out.println('duplicates found');
}
}
我想打印两个项目如下:
找到的重复项目,[key1]带值[“ 123”,“ 456”]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为,对于您试图实现
map&lt; string,list&lt; gt;&gt;
而不是hashset&lt; string&gt;
。实际上,set
只会告诉您键是否已包含在set
中,但它不会跟踪特定键是多少值绑定到。这是一个实现,将密钥对象用作
MAP
的密钥,其值添加到MAP
的值列表中:相反,如果您需要添加数据结构中的实际对象,然后无论使用
hashset
或hashmap
,您的课程都需要通过为该一般的等价和哈希码合同来尊重一般的等价合同,以提供适当的定义相应的方法。https://docs.oracle。 com/javase/7/docs/api/java/lang/object.html#hashcode()
实际上,
hashset
在引擎盖下使用hashmap
,因此在两种情况下都必须覆盖所述方法。这是因为hashmap
是作为一个存储库实现的,其中每个条目都基于密钥的hashcode()
将每个条目存储在存储桶中。但是,不同的键可能会产生相同的哈希码,因此可以在同一存储桶中列出多个条目。那时,hashmap
必须诉诸equals()
方法,以在一个对应于输入的条目的存储桶中找到确切的键,以检索或替换元素。这就是为什么提供hashcode()
和equals()
方法的正确定义的正确定义如此至关重要的原因。在您的情况下,可能的实现可能是:
I think that for what you're trying to achieve a
Map<String, List<String>>
would be better suited rather than aHashSet<String>
. In fact, aSet
would only tell you whether a key is already contained in theSet
or not, but it won't keep track of how many values a particular key is bound to.Here is an implementation where the key's object is used as the key for the
Map
and its value added to theMap
's list of values:Instead, if you need to add the actual object within the data structure, then regardless of using a
HashSet
or aHashMap
, your class needs to honor the general equals and hashcode contracts by providing a proper definition for the corresponding methods.https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()
In fact, a
HashSet
uses aHashMap
under the hood, so in both cases is necessary to override said methods. This is because aHashMap
is implemented as an array of buckets, where each entry is stored within a bucket based on the key'shashCode()
. However, different keys may yield same hashcodes, so multiple entries may be listed within a same bucket. At that point, theHashMap
has to resort to theequals()
method to find the exact key within a bucket which corresponds to the inputted entry in order to retrieve or replace an element. This is why it is so crucial to provide a proper definition of thehashCode()
andequals()
methods.https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
In your case, a possible implementation could be:
准备
hashmap
list
喜欢hashmap&lt; string,list&lt; string&gt;&gt;
最后,您可以迭代hashmap,检查arraylist size&gt; 1是否并使用arraylist打印消息
Prepare a
HashMap
ofList
likeHashMap<String,List<String>>
In the end, you can iterate HashMap, check if the ArrayList size >1 and print the message with ArrayList
尝试一下。
和
输出:
Try this.
And
output:
可以在一个语句中使用Java 8流:
替代方案,并有一些帮助者:
Can be done in one statement using Java 8 streams:
Alternative, with some helpers: