简体   繁体   English

是否可以在Java中使用哈希集过滤掉数组列表中的重复项,而不会扭曲数组的顺序?

[英]Can I filter out duplicates within an array list using hashset in Java without distorting the order of the array?

I was wondering how I could return an array list in Java but filter out any duplicates that come from my object? 我想知道如何在Java中返回数组列表,但过滤掉来自对象的所有重复项? I've heard the best way to do this is by using a hashset is this true or is there a better implementation? 我听说做到这一点的最佳方法是使用哈希集,这是真的还是有更好的实现? The reason I am skeptical about using hashset is I have heard it distorts the order of the array which is not what I want. 我对使用哈希集表示怀疑的原因是,我听说它扭曲了数组的顺序,这不是我想要的。

What you are are looking for is a Collection that maintains insertion order and does not allow duplicates. 您正在寻找的是一个保持插入顺序且不允许重复的Collection

Thankfully, there is one available in the form of a LinkedHashSet 幸运的是,有一个可用LinkedHashSet的形式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM