去除arraylist容器中的相同的對象元素的方法
<span class="keyword" style="background-color: rgb(250, 250, 250); font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace;">boolean</span><span style="color: black; background-color: rgb(250, 250, 250); font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace;"> retainAll(Collection<?> c); </span>
在網(wǎng)上查到了 retainAll方法
然后就在代碼中添加
<pre name="code" class="html">List<Employee> <span style="font-family: Arial, Helvetica, sans-serif;">employeeOfIntermediarys </span><span style="font-family: Arial, Helvetica, sans-serif;">= new ArrayList<Employee>(); </span><pre name="code" class="html">List<Employee> <span style="font-family: Arial, Helvetica, sans-serif;">tempList </span><span style="font-family: Arial, Helvetica, sans-serif;">= new ArrayList<Employee>();</span>
if(tempList != null){
<span style="white-space:pre"> </span>//去掉<span style="font-family: Arial, Helvetica, sans-serif;">tempList 中與</span><span style="font-family: Arial, Helvetica, sans-serif;">employeeOfIntermediarys 中</span>相同的元素,在合并
List<Employee> eoiList = new ArrayList<Employee>(employeeOfIntermediarys);
if (eoiList.retainAll(tempList)) {
tempList.removeAll(eoiList);
}
employeeOfIntermediarys.addAll(tempList); }
然后發(fā)現(xiàn)不可以,查了好多東西,發(fā)現(xiàn)代碼是對的,只是需要重寫equals方法。
不知道為什么,因為之前寫的小demo中用自定義的類book,如下,是可以正常使用的
public class Book {
private int id;
private String nameString;
private String writer;
public Book(){
id= -1;
nameString = "";
writer = "";
}
List<Book> books = new ArrayList<Book>();
List<Book> tempList = new ArrayList<Book>();
Book book1 = new Book();
book1.setId(1);
book1.setNameString("dkjdf");
Book book2 = new Book();
book2.setId(2);
book2.setNameString("到加福祿壽");
Book book3 = new Book();
book3.setId(3);
book3.setNameString("dj地方");
Book book4 = new Book();
book4.setId(4);
book4.setNameString("dkjdf");
Book book5 = new Book();
book5.setId(5);
book5.setNameString("到加福祿壽");
Book book6 = new Book();
book6.setId(6);
book6.setNameString("dj地方");
books.add(book1);
books.add(book2);
books.add(book5);
books.add(null);
tempList.add(book1);
tempList.add(book4);
tempList.add(null);
tempList.retainAll(books);
System.out.println(tempList.get(0).booktoString());
whatever,準(zhǔn)備找一找collection的源代碼看看,
重寫了Employee中的equals方法后 。
如下:
@Override
public boolean equals(Object c){
if (!(c instanceof Employee)) {
return false;
}
Employee eoi = (Employee) c;
if (eoi.getUserGuid().equals(userGuid)) {
return true;
}
return false;
}
上面的代碼就可以正常運(yùn)行了。
以上就是小編為大家?guī)淼娜コ齛rraylist容器中的相同的對象元素的方法的全部內(nèi)容了,希望對大家有所幫助,多多支持腳本之家~
相關(guān)文章
淺析Android企業(yè)級開發(fā)數(shù)據(jù)綁定技術(shù)
這篇文章通過代碼實例分析了Android企業(yè)級開發(fā)數(shù)據(jù)綁定技術(shù)的應(yīng)用以及相關(guān)的原理知識,跟著小編一起學(xué)習(xí)參考下吧。2017-12-12
android中創(chuàng)建通知欄Notification代碼實例
這篇文章主要介紹了android中創(chuàng)建通知欄Notification代碼實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-05-05
分享Android中ExpandableListView控件使用教程
這篇文章主要介紹了Android中ExpandableListView控件使用教程,可以實現(xiàn)二級列表展示效果,需要的朋友可以參考下2015-12-12
Android音頻系統(tǒng)AudioTrack使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android音頻系統(tǒng)AudioTrack的使用方法,如何使用AudioTrack進(jìn)行音頻播放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Android使用Intent傳遞組件大數(shù)據(jù)
這篇文章主要介紹了Android使用Intent傳遞組件大數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容詳情,感興趣的朋友可以參考一下2022-07-07

