What is TreeSet?
TreeSet is similar to HashSet except that it sorts the elements in the ascending order while HashSet doesn’t maintain any order.
Like most of the other collection classes this class is also not synchronized.
How to Perform Basic Operations in TreeSet?
Basic Operations are:
- creating object
- adding elements to it
- verifies elements existence
- deleting all elements at once
- size of the set and deleting a specific element
Basic Operations
import java.util.TreeSet; public class MyBasicTreeset { public static void main(String a[]){ TreeSetTry it Yourselfts = new TreeSet (); 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); } }
How to create a TreeSet with a List?
By passing another collection to the TreeSet constructor, we can copy entire collections elements to the TreeSet.
Example:
import java.util.ArrayList; import java.util.List; import java.util.TreeSet; public class MySetWithCollection { public static void main(String a[]){ ListTry it Yourselfli = new ArrayList (); li.add("one"); li.add("two"); li.add("three"); li.add("four"); System.out.println("List: "+li); //create a treeset with the list TreeSet myset = new TreeSet (li); System.out.println("Set: "+myset); } }
How to read objects from TreeSet using using Iterator?
By calling iterator() method you will get Iterator object, through which you can iterate all the elements of the TreeSet.
Example:
import java.util.Iterator; import java.util.TreeSet; public class MySetIteration { public static void main(String a[]){ TreeSetTry it Yourselfts = new TreeSet (); ts.add("one"); ts.add("two"); ts.add("three"); Iterator itr = ts.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
How to find duplicate value from an array?
To find duplicate entries from the given array is, create TreeSet object and add array entries to the TreeSet.
Since the set doesnot support duplicate entries, you can easily findout duplicate entries.
Example:
import java.util.TreeSet; public class MyDuplicateEntry { public static void main(String a[]){ String[] strArr = {"one","two","three","four","four","five"}; TreeSetTry it Yourselfunique = new TreeSet (); for(String str:strArr){ if(!unique.add(str)){ System.out.println("Duplicate Entry is: "+str); } } } }
Solve this!!
#20807,20808,20809,20810- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling