TreeSet in java

In this post, we will see TreeSet in java.
Java TreeSet have following properties:

  • It can contain only unique element.
  • It stores objects in ascending order by default,
  • It implements NavigableSet interface which extends SortedSet.
  • When you put objects in TreeSet, it must implement Comparable interface.

Lets understand it more with the help of example.

Example:

1) create a class named Country.java.


2) create TreeSetMain.java as below:

When you run above program, you get below out:

As you can see, we have added 6 country objects in Treeset but we have only 4!!. As TreeSet can only have unique elements,indiaCountry2 and nepalCountry2 did not add to the TreeSet. Explanation:

When you will put indiaCountry2 in treeSet it will compare this object with indiaCountry as indiaCountry2.compareTo(indiaCountry) and compareTo method will return 0. Both object will treated as equal. As you can not add duplicate element in TreeSet, indiaCountry2 will not be added to above TreeSet.

Now put debug point at line 27 and right click on project->debug as-> java application. Program will stop execution at line 27 then right click on countryTreeSet then select watch.You will be able to see structure as below.

TreeSet in java

Tricky question on TreeSet: 

Guess output of below program:

What will be output of above program:
A. Martin
B. John
C. John
Martin
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.

Correct Answer is B here.

Explanation:

As you can see we have overridden compareTo method in Employee class and always return 0.
Following steps will take place:

  • First element with “John” will be added to employeeSet.
  • When we will added second element with martin, compareTo method will get called with employeeOne.compareTo(employeeTwo) and it will return 0.
  • As compareTo method returns 0, employeeOne is equals to employeeTwo, so employeeTwo will not be added to treeSet.
  • So output of above program is “John”
Please go through java interview programs for more such programs.

import_contacts

You may also like:

Related Posts

  • 02 October

    Initialize ArrayList with values in Java

    In this article, we will learn to initialize ArrayList with values in Java. ArrayList is an implementation class of List interface in Java. It is used to store elements. It is based on a dynamic array concept that grows accordingly. We can Initialize ArrayList with values in several ways. Let’s see some of them with […]

  • 17 March

    PriorityQueue in Java 8

    In this post, we will see about Java 8 PriorityQueue. When the objects are supposed to be processed on the basis of their priority, in that scenario we use PriorityQueue. It’s a special type of queue (also, unbound queues) where the elements can be ordered either as per their natural ordering or based on a […]

  • 06 October

    2d Arraylist java example

    In this post, we will see how to create 2d Arraylist in java. Best way to create 2d Arraylist is to create list of list in java. [crayon-6001cecac0dc4125170037/] Let’s create a program to implement 2d Arraylist java. [crayon-6001cecac0dc9464557875/] Output: 2nd element in list3 : List3_Str2 3nd element in list1 : List1_Str3 1st element in list2 […]

  • 04 May

    How HashMap works in java

    Most common interview questions are <code>How HashMap works in java</code>, “How get and put method of HashMap work internally”. Here I am trying to explain internal functionality with an easy example. [crayon-6001cecacff9a501193696-i/]  is one of the most used Collections in java.Rather than going through theory, we will start with example first, so that you will […]

  • 21 October

    How to Sort HashSet in Java

    In this post, we will see how to sort HashSet in java. HashSet is a collection which does not store elements in any order. You might come across a situation where you need to sort HashSet. There can be many ways to sort HashSet, we will see two methods here. Using TreeSet You can use […]

  • 20 October

    Java Array to Set

    In this post, we will learn java array to set conversion. There are many ways to convert array to set. 1. Using Java 8’s Stream If you are using Java 8, I would recommend using this method. [crayon-6001cecac0fdc528873901/] Output [John, Martin, Mary] 2. Using HashSet constructor() We can directly call HashSet‘s constructor for java set […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.