How do I go about repeating objects in HashSet?

Source: Internet
Author: User
Tags set set

Welcome to join my QQ Exchange Group 425783133

The summary uses HashSet to implement the set collection, where the objects in the collection are duplicated and the recurrence condition is custom.

How to get rid of duplicate objects in set.

Programme I

Overrides its equals and hashcode for the bean.
       @Override       public int hashcode ()  {             return 
Gradetype.hashcode ();      &NBSP}        @Override        public boolean equals (object obj)  {              if (Obj instanceof examgradestatebean)  {                   examgradestatebean
 state =  (Examgradestatebean)  obj;                   
 return  (State. gradetype.equals ( gradetype));             }     &nbSp;        return super.equals (obj);       }

Write a method that filters the duplicate data in the set.

Suppose that there is a duplicate Set set<person> set = new hashset<person> ();
Set.add ( new person (1,  "P1"));
Set.add ( new person (2,  "P2"));
Set.add ( new person (3,  "P3"));
Set.add ( new person (4,  "P2"));
Set.add ( new person (5,  "P3"));
Set.add ( new person (6,  "P4")); Method of removing duplicate data in Set private static set<person> removeduplicate (Set<person> set)  {    map<string, person> map = new hashmap<string,
 Person> ();
    Set<Person> tempSet = new HashSet<Person> ();          for (person p : set)  {               if (Map.get (P.getName ())  ==  null )  {          &nbsP;        map.put (P.getname (),  p);                        
                } else {                   
 tempset.add (P);               }     
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     set.removeall (Tempset);
    return set; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.