In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.