删除 ArrayList> 中的重复行
public void removeDuplicates (ArrayList<ArrayList<String>> strings) {
Set<ArrayList<String>> s = new LinkedHashSet<ArrayList<String>>(strings);
strings = new ArrayList<ArrayList<String>>(s);
}
我想删除 ArrayListLinkedHashSet
并比较 ArrayList
之间的情况,如果是同样不插入。我知道我需要 Comparator
但我不知道如何实现它来比较 ArrayList
内的 ArrayList
。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试使用 Set<列表> - 如果您实现自己的 List 或扩展 ArrayList 操作系统,那么“equals()”将按您期望的方式运行。
因此,当您添加 arrayList 时,它将自动与其余的进行比较。
不确定先添加 arrayList 然后用元素填充它们时会发生什么。
你的情况是什么?
您说您想使用 LinkedHashSet - 好的,这与我最初的建议相同。
(你不需要比较器)
您可以扩展 ArrayList 并使其“equals()”方法符合您的愿望:
You could try using a Set< List > - and if you implement your own List or extend ArrayList os that the "equals()" acts the way you expect.
So when you add an arrayList it will automatically be compared with the rest.
Not sure what will happen when you add the arrayLists first and then populate them with elements.
What is your case?
You said you want to use LinkedHashSet - ok, that is the same as my initial suggestion.
(you don't need comparator)
You can extend ArrayList and make its "equals()" method conform your wish:
您没有提到订单是否重要。
如果内部列表中的项目顺序不重要,则这应该有效:-
You didn't mention if the order is important.
If the item order in the inner list is not important, this should work:-
您将必须进行强力比较,并将每个数组列表与其他每个数组列表进行比较。
并将其元素与索引 1-n 进行比较
从索引 0 开始,一旦找到一个遵循伪代码的假设 ArrayList myDatastruct
:对于比较方法,您将需要编写一个 for 循环,逐一迭代比较每个索引处的项目在两个 arrayList 中,如果它们的长度不同,您将希望通过不费心比较它们来短路这一点。
您可能希望在单独的数组列表中标记要删除的索引,并在单独的循环中删除它们,否则您会搞砸索引。
实施应该相当简单,因此留作练习。
You are going to have to do a brute force compare and compare each and every arraylist against every other array list.
start with index 0 and compare it's elements against index 1-n once you find an
Assumeing ArrayList myDatastructure following psuedo code:
for the compare method you will want to write a for loop that iterates through the elements one by one comparing the item at each index in both arrayLists, you will want to short circuit this by not bothering to compare them if they are not the same length.
You will probably want to mark the indexes that you want to remove in an Separate arraylist and remove them in a separate loop otherwise you will screw up your indices.
Implementation should be fairly simple, so that's left as an exercise.
假设该列表包含
您期望该集合包含什么?两个元素?一种元素?
你能说出你想如何比较 ArrayList 吗?
如果这是一对一的比较 - “如果它们以相同顺序包含相同元素,那么它们就相等”,那么
应该足够了。
如果您想要更复杂的比较规则,那么您需要重写 equals 方法,如 Leni Kirilov 上面所说。但列表越大,性能开销就会越高。
Say the list contains
What do you expect the set to contain? Two elements? One element?
Can you tell how you want to compare the ArrayLists?
If that is the one-to-one compare - "they are equal if they contain the same elements in the same order" then
Should be sufficient.
If you want to have more complex comparison rules then you need to override the equals method as Leni Kirilov said above. But the larger the list is, you will have a high performance overhead.
同意上面的@Leni,只需重写 ArrayLists 的 equals 方法,并且 LinkedHashSet 实现应该自动过滤重复项。
来自 Java 文档:不包含重复元素的集合。更正式地讲,集合不包含使 e1.equals(e2) 的一对元素 e1 和 e2,并且最多包含一个空元素。正如其名称所暗示的,该接口对数学集合抽象进行建模
Agree with @Leni above, just override ArrayLists's equals method and the LinkedHashSet implementation should automatically filter duplicates.
From Java doc: A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction