java的删除ArrayList中重复的对象 [英] Java remove duplicate objects in ArrayList
本文介绍了java的删除ArrayList中重复的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我却包含对象的一些人一个非常漫长的ArrayList,无疑是复制。这是发现和除去这些重复的最佳方式。注:我已经写了布尔返回compareObjects()方法
I have a very lengthy ArrayList comprised of objects some of them however, are undoubtedly duplicates. What is the best way of finding and removing these duplicates. Note: I have written a boolean-returning compareObjects() method.
推荐答案
请使用一组prevents存储重复在首位
例如:
Please use a Set which prevents storage of duplicates at the first place Example:
List<Item> result = new ArrayList<Item>();
Set<String> titles = new HashSet<String>();
for( Item item : originalList ) {
if( titles.add( item.getTitle() ) {
result.add( item );
}
}
Add()方法,如果该元素已经存在的设置返回false。
add() of the Set returns false if the element already exists.
贷@Aaron Digulla
Credits to @Aaron Digulla
这篇关于java的删除ArrayList中重复的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文