Easy Tutorial
For Competitive Exams

Java Programming TreeSet

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[]){
       TreeSet ts = 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);
    }
}
Try it Yourself

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[]){
        List li = 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);
    }
}
Try it Yourself

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[]){
		
		TreeSet ts = new TreeSet();
		ts.add("one");
		ts.add("two");
		ts.add("three");
		Iterator itr = ts.iterator();
		while(itr.hasNext()){
			System.out.println(itr.next());
		}
	}
}
Try it Yourself

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"};
        TreeSet unique = new TreeSet();
        for(String str:strArr){
            if(!unique.add(str)){
                System.out.println("Duplicate Entry is: "+str);
            }
        }
    }
}
Try it Yourself

Solve this!!

#20807,20808,20809,20810
Share with Friends