這篇文章主要介紹了java怎么篩選list中相同的值和篩選不同的值的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇java怎么篩選list中相同的值和篩選不同的值文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、成都小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了洞頭免費(fèi)建站歡迎大家使用!
代碼如下:
public class People { private String id; private String somethingElse; public People() { } public People(String id, String somethingElse) { this.id = id; this.somethingElse = somethingElse; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSomethingElse() { return somethingElse; } public void setSomethingElse(String somethingElse) { this.somethingElse = somethingElse; } @Override public String toString() { return "People{" + "id='" + id + '\'' + ", somethingElse='" + somethingElse + '\'' + '}'; } }
people實(shí)體類和student實(shí)體類
People類
student public class Student { private String id; private String idCard; private String somethingElse; public Student() { } public Student(String id, String idCard, String somethingElse) { this.id = id; this.idCard = idCard; this.somethingElse = somethingElse; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getSomethingElse() { return somethingElse; } public void setSomethingElse(String somethingElse) { this.somethingElse = somethingElse; } @Override public String toString() { return "Student{" + "id='" + id + '\'' + ", idCard='" + idCard + '\'' + ", somethingElse='" + somethingElse + '\'' + '}'; } }
測(cè)試類
public static void main(String[] args) throws Exception{ //初始化數(shù)據(jù) ListstudentList = new ArrayList(){{ add(new Student("1", "11", "111")) ; add(new Student("2", "22", "222")); add(new Student("3", "33", "333")); }}; List peopleList = new ArrayList(){ { add(new People("11", "111")); add(new People("222", "222")); add(new People("33", "333")); }}; //獲取相同字段內(nèi)容,轉(zhuǎn)化為set Set ids = peopleList .stream() .map(People::getId) .collect(Collectors.toSet());//過(guò)濾重復(fù)內(nèi)容 List result = studentList .stream() .filter(e -> ids.contains(e.getIdCard())) .collect(Collectors.toList()); System.out.println(result); //獲取相同字段內(nèi)容,轉(zhuǎn)化為set Set ids = peopleList .stream() .map(People::getId) .collect(Collectors.toSet());//過(guò)濾重復(fù)內(nèi)容 List result = studentList .stream() .filter(e -> !ids.contains(e.getIdCard())) .collect(Collectors.toList()); System.out.println(result); }
關(guān)于“java怎么篩選list中相同的值和篩選不同的值”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“java怎么篩選list中相同的值和篩選不同的值”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。