Remove repeated custom object elements from ArrayList.

Source: Internet
Author: User

Remove repeated custom object elements from ArrayList.

1/* 2 * Custom object Person class 3 */4 public class Person {5 private String name; 6 private int age; 7 8 public Person (String name, int age) {9 super (); 10 this. name = name; 11 this. age = age; 12} 13 14 public String getName () {15 return name; 16} 17 18 public void setName (String name) {19 this. name = name; 20} 21 22 public int getAge () {23 return age; 24} 25 26 public void setAge (int age) {27 this. age = age; 28} 29 30 @ Override31 public String toString () {32 return "Person [name =" + name + ", age =" + age + "]"; 33} 34 35 @ Override36 public boolean equals (Object obj) {// rewrite the equals method 37 Person per = (Person) obj; 38 if (per. getName (). equals (this. name) & per. getAge () = this. age) {39 return true; 40} 41 return false; 42} 43 44}

 

1 import java. util. arrayList; 2 import java. util. listIterator; 3 4 import com. heima. bean. person; 5 6/* 7 * requirement: ArrayList removes repeated values of custom object elements in the Set (the value of the object's member variables is the same) 8 * idea: override equals () method 9 */10 public class Demo2_ArrayList {11 public static void main (String [] args) {12 ArrayList <Person> list = new ArrayList <Person> (); 13 list. add (new Person ("Zhang San", 23); 14 list. add (new Person ("Li Si", 24); 15 list. add (new Person ("Wang Wu", 25 ); 16 list. add (new Person ("Zhao ", 26); 17 list. add (new Person ("Zhang San", 23); 18 list. add (new Person ("Li Si", 24); 19 list. add (new Person ("Zhao ", 26); 20 System. out. println (getSingle (list); 21} 22 23 public static ArrayList <Person> getSingle (ArrayList <Person> list) {24 ArrayList <Person> newArrayList = new ArrayList <Person> (); 25 ListIterator <Person> iter = list. listIterator (); 26 while (iter. hasNext () {27 Pers On per = (Person) iter. next (); 28 if (! NewArrayList. contains (per) {// The contains method determines whether the content is included. The underlying layer depends on the equals method 29 newArrayList. add (per); // The remove method determines whether to delete the file. The underlying layer depends on the equals Method 30} 31} 32 33 return newArrayList; 34 35} 36 37}

 

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.