1. Can the list set add elements repeatedly? How to remove duplicate elements
Solution ideas
-
Traverse the original set
-
-
Sort first and then traverse the set. If the two adjacent elements are equal, delete the latter one.
-
Here's a mistake we often make, just right This article The blog provides the perfect solution.
-
-
Convert to Set collection
-
- Create a subclass of any Set set Set, pass our list as a parameter to the construction method of the Set collection subclass, then create a subclass of the list Set, pass our Set as a parameter to the construction method of the list Set, and then overwrite the original list with the new list
-
- Create a set set, call the addAll(list) method, then empty our List, and then call addAll (set)
Traverse the original set
public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(3); list.add(3); list.add(5); list.add(1); //Arrange first Collections.sort(list); //ergodic Iterator<Integer> iterator = list.iterator(); //Mark previous element int last=0; while (iterator.hasNext()){ int value=iterator.next(); if (value==last){ iterator.remove(); } else { last=value; } } //Print results System.out.println(list); }
Operation results
[1, 3, 5]
Three methods of converting to set
- Construction method transformation
public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(3); list.add(3); list.add(5); list.add(1); //transformation TreeSet<Integer> set = new TreeSet<>(list); list=new ArrayList<>(set); //Print results System.out.println(list); }
- addAll method conversion
public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(3); list.add(3); list.add(5); list.add(1); //Create set set TreeSet<Integer> set = new TreeSet<>(); set.addAll(list); //empty list.clear(); list.addAll(set); //Print results System.out.println(list); }
- There are still many methods, but the general idea is to put all the elements of the list into the set and return them to the list
2. Can custom classes be directly saved into TreeSet? If not, how to solve it?
We give a case
public class User { private String userName; private String password; private String userRole; get set ...... }
We try to pass in User into TreeSet
public static void main(String[] args) { TreeSet<User> users = new TreeSet<>(); users.add(new User("faker","123456")); users.add(new User("jakt","123456")); users.add(new User("paul","123456")); }
Operation results
Exception in thread "main" java.lang.ClassCastException: com.***.test01.User cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(TreeMap.java:1294) at java.util.TreeMap.put(TreeMap.java:538) at java.util.TreeSet.add(TreeSet.java:255) at com.kaikeba.test01.Test7.main(Test7.java:15)
The exception means that this class has no comparability and needs to implement the Comparable interface. I provide it here TreeSet underlying analysis Link to.
resolvent
- Implement Comparable interface
- The Comparator comparator is passed in when the TreeSet is instantiated
3. What are the sorting methods of user-defined classes in the collection and what are they?
Inheritance interface
Here, I copied the equals () method and thought that as long as the username s of two users are the same, they are the same object. If the types of the two objects in the chain are different, I will throw an exception directly.
If it is not the same object, it indicates that the usernames are inconsistent. Our compareTo directly returns the difference between the hash values of the usernames of the two objects
public class User implements Comparable { private String userName; private String password; private String userRole; @Override public int compareTo(Object o) { if (equals(o)){ return 0; } else { User user=(User)o; return userName.hashCode()-user.getUserName().hashCode(); } } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { throw new ClassCastException("Different types!"); } User user = (User) o; return userName.equals(user.userName); } }
test method
public static void main(String[] args) { TreeSet<User> users = new TreeSet<>(); users.add(new User("faker","123456")); users.add(new User("faker","123")); users.add(new User("paul","123456")); System.out.println(users); }
test result
Note that if two objects are found to be consistent, we will discard the addition instead of overwriting!
[User [userName=paul, password=123456, userRole=Ordinary users], User [userName=faker, password=123456, userRole=Ordinary users]]
Define comparator
Here I write the anonymous implementation class directly
public static void main(String[] args) { TreeSet<User> users = new TreeSet<>(new Comparator<User>() { @Override public int compare(User o1, User o2) { if (o1.equals(o2)){ return 0; } else{ return o1.getUserName().hashCode()-o2.getUserName().hashCode(); } } }); users.add(new User("123","Cheng Haonan")); users.add(new User("123","Situ Haonan")); users.add(new User("paul","123456")); System.out.println(users); }
test result
There is only one Haonan in Causeway Bay, that is my Cheng Haonan!!!
[User [userName=paul, password=123456, userRole=Ordinary users], User [userName=faker, password=123456, userRole=Ordinary users]]