Change the ArrayList into TreeSet.
时间: 2024-05-11 22:19:24 浏览: 120
Sure, here's an example of how to change an ArrayList into a TreeSet:
```
import java.util.ArrayList;
import java.util.TreeSet;
public class Example {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(5);
arrayList.add(2);
arrayList.add(8);
arrayList.add(1);
arrayList.add(9);
// Convert the ArrayList to a TreeSet
TreeSet<Integer> treeSet = new TreeSet<>(arrayList);
// Print the TreeSet
System.out.println(treeSet);
}
}
```
In this example, we first create an ArrayList of integers. We then create a TreeSet and pass the ArrayList to its constructor. This automatically creates a sorted set with the elements of the ArrayList. Finally, we print the TreeSet to verify that it is sorted.
阅读全文
相关推荐


















