TreeSets – Java Collections Framework

Hello coders, I am back with another article in my Java Collections Framework article series. Today I am going to discuss our 3rd and final class that implement the Set Interface which is TreeSet class. TreeSet is quite similar to the HashSet class we talked about earlier.

Since you already know about the HashSet class, I am going to discuss the similarities and differences between the two classes to give you a clearer idea.

Similarities

1. Doesn’t allow duplicate elements

Both HashSet and TreeSet objects do not allow duplicate elements. If we insert duplicate elements, the existing elements will be overwritten.

        //Creating TreeSet and HashSet objects
        TreeSet<String> myTreeSet = new TreeSet<>();
        HashSet<String> myHashSet = new HashSet<>();

        //Adding elements to the TreeSet
        myTreeSet.add("Nicola Tesla");
        myTreeSet.add("Elon Musk");
        myTreeSet.add("Thomas Edison");
        myTreeSet.add("Gary V");
        myTreeSet.add("Elon Musk");
        System.out.println(myTreeSet);

        //Adding elements to the TreeSet
        myHashSet.add("Nicola Tesla");
        myHashSet.add("Elon Musk");
        myHashSet.add("Thomas Edison");
        myHashSet.add("Gary V");
        myHashSet.add("Elon Musk");
        System.out.println(myHashSet);

Here is the console output. If you look carefully, you might be able to notice one of the differences between these two classes as well. Don’t worry if you don’t. I am going to explain it in the latter part of the article anyway.

[Elon Musk, Gary V, Nicola Tesla, Thomas Edison]
[Thomas Edison, Gary V, Nicola Tesla, Elon Musk]
2. Both are not synchronized

Both classes are non-synchronized classes which means that they are not thread-safe.

Differences

1. Maintaining an order

HashSet class do not maintain order while the TreeSet class maintains the ascending order. If we look at the earlier code snippet, we can clearly see the difference. We can see the TreeSet maintaining the ascending order while HashSet don’t.

[Elon Musk, Gary V, Nicola Tesla, Thomas Edison]
[Thomas Edison, Gary V, Nicola Tesla, Elon Musk]
2. Performance

When it comes to performance, the HashSet class is in front. HashSet class provides a faster performance when performing general CRUD operations.

Converting HashSets into TreeSets

If you want, you can convert HashSets into TreeSets. This can be useful when you want to sort the elements in a HashSet. You can simply convert your HashSet into a TreeSet in order to sort the elements in ascending order.

        //Creating TreeSet and HashSet objects
        HashSet<String> myHashSet = new HashSet<>();

        //Adding elements to the TreeSet
        myHashSet.add("Mahela");
        myHashSet.add("Sanga");
        myHashSet.add("Dasun");
        myHashSet.add("Mavan");
        myHashSet.add("Dilshan");
        System.out.println(myHashSet);

        TreeSet<String> myTreeSet = new TreeSet<>(myHashSet);
        System.out.println(myTreeSet);

Here is the console output.

[Dasun, Sanga, Mavan, Dilshan, Mahela]
[Dasun, Dilshan, Mahela, Mavan, Sanga]

If you want to know more about the TreeSet class, refer to the Java Documentation.


This ends the article about the TreeSet class. If you liked the article, drop a like and follow my blog. If you have any doubts, drop a comment down below. Stay Safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: