Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods of removing duplicate objects by java8

Shulou Source: shulou.com Published: 2022-06-01 00:23:34 09月18日 Update

Most people do not understand the knowledge points of this article, "what are the methods for java8 to remove repetitive objects?", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what are the methods for java8 to remove repetitive objects?"

First, remove repeated Stringpublic List removeStringListDupli (List stringList) {Set set = new LinkedHashSet (); set.addAll (stringList); stringList.clear (); stringList.addAll (set); return stringList;} in List

Or use Java8 to write:

List unique = list.stream (). Distinct (). Collect (Collectors.toList ()); 2. Object de-duplication in List

For example, there is now a Person class:

Public class Person {private Long id; private String name; public Person (Long id, String name) {this.id = id; this.name = name;} public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;} @ Override public String toString () {return "Person {" + "id=" + id + ", name='" + name +'\'+'}';}}

Override the equals () method and hashCode () method of the Person object:

@ Override public boolean equals (Object o) {if (this = = o) return true; if (o = = null | | getClass ()! = o.getClass () return false; Person person = (Person) o; if (! id.equals (person.id)) return false; return name.equals (person.name);} @ Override public int hashCode () {int result = id.hashCode () Result = 31 * result + name.hashCode (); return result;}

The following object deduplicates the code:

Person p1 = new Person (11, "jack"); Person p2 = new Person (31, "jack chou"); Person p3 = new Person (21, "tom"); Person p4 = new Person (41, "hanson"); Person p5 = new Person (51, "glue worm"); List persons = Arrays.asList (p1, p2, p3, p4, p5, p5, p1, p2, p2); List personList = new ArrayList () / / deduplicated persons.stream () .forEach (p-> {if (! personList.contains (p)) {personList.add (p);}}); System.out.println (personList)

The underlying implementation of the contains () method of List uses the object's equals method to compare. In fact, it would be nice to rewrite equals (), but if you rewrite equals, you'd better rewrite hashCode as well.

Third, weight it according to the attributes of the object.

Next, we need to de-weight according to the id of the Person object, so what should we do?

Write a method:

Public static List removeDupliById (List persons) {Set personSet = new TreeSet ((o1, O2)-> o1.getId (). CompareTo (o2.getId ()); personSet.addAll (persons); return new ArrayList (personSet);}

Through the Comparator comparator, compare the object properties, the same will return 0, to achieve the purpose of filtering.

Let's take a look at the cool way to write Java8:

Import static java.util.Comparator.comparingLong;import static java.util.stream.Collectors.collectingAndThen;import static java.util.stream.Collectors.toCollection;// removes weight according to id List unique = persons.stream () .collect (collectingAndThen (toCollection (()-> new TreeSet (comparingLong (Person::getId), ArrayList::new)

There is another way to write it:

Public static Predicate distinctByKey (Function

Tags: Objects methods content writing attributes articles knowledge articles same code value most of that is bottom more best steps purpose knowledge points tape Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn macOS Apple Redmi