This Java Example shows how to convert an array to Java TreeSet object using Arrays.asList method.
import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.TreeSet; public class ArrayToSetExample { public static void main(String[] args) { Integer[] numbers = { 7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4 }; /** * To convert an array into a Set, first we convert it into a List using * Arrays.asList method. Next we create a TreeSet and pass the list as * an argument to the constructor. **/ List< Integer > list = Arrays.asList(numbers); Set< Integer > set = new TreeSet< Integer >(list); // Display elements of the set System.out.println("The set contains..."); for (Integer n : set) { System.out.println(n); } } }
The output is:
The set contains...
4
5
6
7
8
9
10
Nice Article. visit more java examples
ReplyDeleteThis is nice post about conversion from array to treeSet .
ReplyDeleteThanks for this nice post.
Thanks for sharing good post.I am sharing good article for Java TreeSet Examples
ReplyDelete