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.
推荐答案
示例
List<Item> result = new ArrayList<Item>();
Set<String> titles = new HashSet<String>();
for( Item item : originalList ) {
if( titles.add( item.getTitle() )) {
result.add( item );
}
}
参考
这篇关于Java删除ArrayList中的重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文