去除arraylist容器中的相同的對(duì)象元素的方法
<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)代碼是對(duì)的,只是需要重寫equals方法。
不知道為什么,因?yàn)橹皩懙男emo中用自定義的類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容器中的相同的對(duì)象元素的方法的全部內(nèi)容了,希望對(duì)大家有所幫助,多多支持腳本之家~
相關(guān)文章
android圖片類型之間相互轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了android圖片類型之間相互轉(zhuǎn)換的方法,涉及Android實(shí)現(xiàn)各種常用圖片類型及字節(jié)類型的轉(zhuǎn)換技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10android開發(fā)socket編程之udp發(fā)送實(shí)例分析
這篇文章主要介紹了android開發(fā)socket編程之udp發(fā)送,實(shí)例分析了Android開發(fā)socket網(wǎng)絡(luò)編程中udp發(fā)送的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
這個(gè)方法是根據(jù)傳入的圖片的高度(height)和寬度(width)決定的,如果是 width <= height時(shí),則會(huì)裁剪高度,裁剪的區(qū)域是寬度不變高度從頂部到寬度width的長度2013-01-01詳解Android 在 ViewPager 中使用 Fragment 的懶加載
本篇文章主要介紹了Android 在 ViewPager 中使用 Fragment 的懶加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android開發(fā)實(shí)現(xiàn)的圖片點(diǎn)擊切換功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的圖片點(diǎn)擊切換功能,涉及Android ImageView組件創(chuàng)建、布局及實(shí)現(xiàn)圖形切換相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Android?Studio打包?aar實(shí)現(xiàn)步驟示例詳解
這篇文章主要為大家介紹了Android?Studio打包aar步驟示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android Intent傳遞數(shù)據(jù)大小限制詳解
這篇文章主要給大家介紹了關(guān)于Android Intent傳遞數(shù)據(jù)大小限制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Android實(shí)現(xiàn)用代碼簡單安裝和卸載APK的方法
這篇文章主要介紹了Android實(shí)現(xiàn)用代碼簡單安裝和卸載APK的方法,涉及Android針對(duì)APK文件及package的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08下載、編譯、運(yùn)行android 7.1系統(tǒng)詳解(ubuntu 16.0.4)
Android 7的系統(tǒng)版本新增的很多的新功能,本篇文章主要介紹了基于ubuntu 16.0.4環(huán)境的下載、編譯、運(yùn)行android 7.1系統(tǒng),有興趣的可以了解一下。2017-01-01