Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does java filter the same values and different values in list

Shulou Source: shulou.com Published: 2022-05-31 11:02:46 11月12日 Update

This article mainly introduces java how to filter the same value in list and filter different values of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this java how to filter the same value in list and filter different values of the article will have a harvest, let's take a look.

The code is as follows:

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 entity class and student entity class

People class

Studentpublic 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 +'\'+'}';}}

Test class

Public static void main (String [] args) throws Exception {/ / initialization data List studentList = new ArrayList () {{add (new Student ("1", "11", "111")); add (new Student (" 2 "," 22nd ")); 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 "));}} / / get the same field content and convert it to set Set ids = peopleList .stream () .map (People::getId) .fields (Collectors.toSet ()) / / filter duplicates List result = studentList .stream () .filter (e-> ids.contains (e.getIdCard () .filter (Collectors.toList ()); System.out.println (result) / / get the same field content and convert it to set Set ids = peopleList .stream () .map (People::getId) .fields (Collectors.toSet ()) / / filter duplicates List result = studentList .stream () .filter (e->! ids.contains (e.getIdCard () .filter (Collectors.toList ()); System.out.println (result);} this is the end of the article on "how java filters the same values and different values in list". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how java filters the same values and different values in list". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Same content different knowledge fields entities articles code value easy to operate data articles easy to understand more look industry information information channels channels learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info Xiaomi MySQL Apple