TreeSet is a type of Collection, that implements Set Interface. It is mostly similar to HashSet class that is it do not allow duplicate elements to be stored.
In TreeSet all the values are stored in there natural order, like all integer values are stored in ascending order and strings are stored according to Dictionary values. Apart from adding this functionality of maintaining natural ordering, Tree Set do not allow null values.
TreeSet is best choice for storing large amount of data, as its retrieval and access time is very fast, which makes data to be found in no time.
Important Note: Hash set does not maintains any order, that is when we retrieve values from it we do not get that values in the same order we have entered in it.
Table of Contents
TreeSet implements NavigableSet Interface which further extends SortedSet, Set, Collection, and Iterable Interfaces, as shown in figure 1.
Let us discuss TreeSet with the help of program. Following program has been divided into 3 Steps that we will discuss one by one:
import java.util.*; public class TreSetMethodsDemo { public static void main(String args[]){ //Step 1 TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>(); treeSetObject1.add(3); //Step 2 treeSetObject1.add(5); treeSetObject1.add(4); treeSetObject1.add(1); treeSetObject1.add(2); treeSetObject1.add(8); treeSetObject1.add(9); treeSetObject1.add(10); treeSetObject1.add(9); treeSetObject1.add(12); //Step 3 System.out.println("Values in tree Set Object 1 :" +treeSetObject1); } }
Output:
Values in tree Set Object 1:[1, 2, 3, 4, 5, 8, 9, 10, 12]
Description:
1. boolean add(Object o):
This methods adds element in the object, which will automatically stores all the elements in increasing order, as shown in the following program:
import java.util.*; public class TreSetMethodsDemo { public static void main(String args[]){ TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>(); TreeSet <String>treeSetObject2 = new TreeSet<String>(); treeSetObject1.add(3); treeSetObject1.add(5); treeSetObject1.add(5); treeSetObject1.add(1); treeSetObject1.add(2); treeSetObject2.add("B"); treeSetObject2.add("C"); treeSetObject2.add("A"); treeSetObject2.add("D"); treeSetObject2.add("A"); System.out.println("Values in TreeSet object 1: " +treeSetObject1); System.out.println("Values in TreeSet object 2: " +treeSetObject2); } }
Output:
Values in TreeSet object 1: [1, 2, 3, 5] Values in TreeSet object 2: [A, B, C, D]
2. boolean addAll(Collection c):
This method adds all the elements of one object to another , as shown in the following program:
import java.util.*; public class TreSetMethodsDemo { public static void main(String args[]){ TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>(); TreeSet <Integer>treeSetObject2 = new TreeSet<Integer>(); treeSetObject1.add(3); treeSetObject1.add(5); treeSetObject1.add(5); treeSetObject1.add(1); treeSetObject1.add(2); treeSetObject2.add(9); treeSetObject2.add(11); treeSetObject2.add(1); treeSetObject2.add(8); treeSetObject2.add(7); treeSetObject1.addAll(treeSetObject2); System.out.println("Values in TreeSet object 1: " +treeSetObject1); } }
Output:
Values in TreeSet object 1: [1, 2, 3, 5, 7, 8, 9, 11]
3. Object ceiling(Object o):
This method retrieves the least element which is greater than or equal to the given element in the argument list, or null if there is no such element in the set as shown in the following program:
import java.util.*; public class TreSetMethodsDemo { public static void main(String args[]){ TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>(); treeSetObject1.add(3); treeSetObject1.add(5); treeSetObject1.add(5); treeSetObject1.add(1); System.out.println("Ceiling value of '5' is:" +treeSetObject1.ceiling(5)); System.out.println("Ceiling value of '2' is:" +treeSetObject1.ceiling(2)); } }
Output:
Ceiling value of '5' is:5 Ceiling value of '2' is:3
4. void clear():
This method removes all of the elements from this object, as shown in the following program:
import java.util.*; public class TreSetMethodsDemo { public static void main(String args[]){ TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>(); treeSetObject1.add(3); treeSetObject1.add(5); treeSetObject1.add(5); treeSetObject1.add(1); treeSetObject1.add(2); System.out.println("Values in TreeSet object 1:" +treeSetObject1); treeSetObject1.clear(); System.out.println("Values in TreeSet object 1:" +treeSetObject1); } }
Output:
Values in TreeSet object 1:[1, 2, 3, 5] Values in TreeSet object 1:[]
Read All TreeSet Methods In JAVA With Example
Premium Project Source Code:
Leave a Reply