# Daily interview question 5 (List, TreeSet, set sort)

#### 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<>();
//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

1. Construction method transformation
```public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
//transformation
TreeSet<Integer> set = new TreeSet<>(list);
list=new ArrayList<>(set);
//Print results
System.out.println(list);
}
```
```public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
//Create set set
TreeSet<Integer> set = new TreeSet<>();
//empty
list.clear();
//Print results
System.out.println(list);
}
```
1. 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 userRole;
get set ......
}
```

We try to pass in User into TreeSet

```public static void main(String[] args) {
TreeSet<User> users = new TreeSet<>();
}
```

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 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 userRole;

@Override
public int compareTo(Object o) {
if (equals(o)){
return 0;
}
else {
User user=(User)o;
}
}

@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;
}
}
```

test method

```    public static void main(String[] args) {
TreeSet<User> users = new TreeSet<>();
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{
}
}
});
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]]
```

Tags: Java

Posted by NME on Sun, 22 May 2022 14:39:20 +0300