Java TreeSet class is a part of the collection framework which implements the Set interface. The TreeSet integer values are stored in the natural sorting order, and the string values are stored according to the Dictionary values. You may also provide a custom comparator to sort the TreeSet at the time of creation to sort the elements based on the comparator.
As it implements Set interface, null values are not allowed. Like other classes of Collection framework, this class also not synchronized but we can explicitly make it synchronized by using
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
TreeSet Class Important Features
- It Extends AbstractSet class and implements a NavigableSet interface.
- Duplicate Values and NULL values are not allowed
- Here the elements are stored in sorting order.
- TreeSet does not allow to insert heterogeneous objects because it needs to compare the existing object to determine the sorting order.
- TreeSet also implements Serializable and Cloneable interfaces.
- TreeSet class object is not synchronized or not thread-safe, that means if multiple threads are acting on the same class elements, then the result is un-deterministic. To make it synchronized we can use Collections.synchronizedSortedSet(new TreeSet()).
TreeSet ts = new TreeSet();
Set syncSet = Collections.synchronziedSet(ts);
TreeSet Class Constructors
The TreeSet has four possible constructors:
- TreeSet t = new TreeSet(); By using this we can create an empty TreeSet object where the objects will store in the default natural sorting order.
- TreeSet t = new TreeSet(Comparator comp); This constructor is used when your requirement is sorting of the elements.
- TreeSet t = new TreeSet(Collection col); We require this type of constructor when it is necessary to the conversion of collection object to TreeSet Object.
- TreeSet t = new TreeSet(SortedSet s); we are using this constructor when we need to convert SortedSet object to TreeSet Object.
TreeSet Different Methods Used In Java With Example?
package com.java.Softwaretestingblog; import java.util.TreeSet; public class TreeSet_Different_Methods { public static void main(String[] args) { // TODO Auto-generated method stub TreeSet<String> ts = new TreeSet<String>(); ts.add("one"); ts.add("two"); ts.add("three"); System.out.println("Elements: "+ts); //check is set empty? System.out.println("Is set empty: "+ts.isEmpty()); //delete all elements from set ts.clear(); System.out.println("Is set empty: "+ts.isEmpty()); ts.add("one"); ts.add("two"); ts.add("three"); System.out.println("Size of the set: "+ts.size()); //remove one string ts.remove("two"); System.out.println("Elements: "+ts); } }
Output:
Elements: [one, three, two] Is set empty: false Is set empty: true Size of the set: 3 Elements: [one, three]
How to Create TreeSet With List In Java With Example?
package com.java.Softwaretestingblog; import java.util.ArrayList; import java.util.List; import java.util.TreeSet; public class Create_TreeSet_List { public static void main(String[] args) { // TODO Auto-generated method stub List<String> li = new ArrayList<String>(); li.add("one"); li.add("two"); li.add("three"); li.add("four"); System.out.println("List: "+li); //create a treeset with the list TreeSet<String> myset = new TreeSet<String>(li); System.out.println("Set: "+myset); } }
Output:
List: [one, two, three, four] Set: [four, one, three, two]
Remove Duplicate Values TreeSet Java Program: How to Remove Duplicate Values TreeSet Java With Example?
package com.java.Softwaretestingblog; import java.util.TreeSet; public class Remove_Duplicate_Values_TreeSet { public static void main(String[] args) { // TODO Auto-generated method stub String[] strArr = {"one","two","three","four","four","five"}; TreeSet<String> unique = new TreeSet<String>(); for(String str:strArr) { if(!unique.add(str)) { System.out.println("Duplicate Entry is: "+str); } } } }
Output:
Duplicate Entry is: four
Leave a Reply