- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Similarities between TreeMap and TreeSet in Java
The TreeMap and TreeSet, both are the part of Collection Framework classes. There exist a few differences as well as a few similarities in their implementation and working. The TreeMap maintains key-value pair on the other hand the TreeSet does not have this feature. In this article, we will discuss the similarities between both classes of Collection Interface.
Collection Interface
In Java, collection is an object or we can say a container for simplicity that allows us to group several numbers of objects in a single unit. The collection interface is present at the root of all collection framework interfaces.
The following sub-interfaces of collection interface are implemented by TreeMap and TreeSet −
Map Interface − It stores its element in key-value pairs. The Key is an object that is used to fetch and receive value associated with it.
Set − It is the sub interface of java Collection Interface that doesn’t allow duplicate values. It is similar to mathematical set.
TreeMap
It is a class that is used to implement NavigableMap Interface. It stores the elements of the map in a tree structure. It provides an efficient alternative to store the key-value pairs in sorted order.
The general syntax for TreeMap is as follows −
Syntax
TreeMap<TypeOfKey, TypeOfValue> nameOfMap = new TreeMap<>();
TreeSet
It is a class that is used to implement NavigableSet Interface. It stores the elements of the set in a tree structure. All the elements are stored in a sorted manner that reduces the retrieval time.
The general syntax for TreeSet is as follows −
Syntax
TreeSet<TypeOfSet> nameOfSet = new TreeSet<>();
Java Program of TreeMap and TreeSet
Example 1
The following example illustrates the use of TreeSet. We have used a few in-built methods of this class.
import java.util.*; public class Srtset { public static void main(String args[]) { // Creating tree set TreeSet<String> treeSt = new TreeSet<>(); // Adding elements in tree set treeSt.add("Tutorix"); treeSt.add("Simply"); treeSt.add("Easy"); treeSt.add("Learning"); treeSt.add("Tutorials"); treeSt.add("Point"); System.out.println("Elements in the given set: " + treeSt); String frst = treeSt.first(); // to access first element System.out.println("Accessing First element of the given set: " + frst); String end = treeSt.last(); // to access last element System.out.println("Accessing Last element of the given set: " + end); System.out.println("Accessing subsets of the given set: " + treeSt.subSet("Simply", "Tutorix")); System.out.println("Accessing first two elements of set: " + treeSt.headSet("Point")); System.out.println("Accessing last three elements of set: " + treeSt.tailSet("Simply")); } }
Output
Elements in the given set: [Easy, Learning, Point, Simply, Tutorials, Tutorix] Accessing First element of the given set: Easy Accessing Last element of the given set: Tutorix Accessing subsets of the given set: [Simply, Tutorials] Accessing first two elements of set: [Easy, Learning] Accessing last three elements of set: [Simply, Tutorials, Tutorix]
Example 2
The following example illustrates the implementation of TreeMap. We have used a few in-built methods of this class.
import java.util.*; public class Srt { public static void main(String[] args) { TreeMap<String, Integer> workers = new TreeMap<>(); // Adding elements in the workers map workers.put("Vaibhav", 4000); workers.put("Ansh", 3000); workers.put("Vivek", 1500); workers.put("Aman", 2000); workers.put("Tapas", 2500); // printing details workers map System.out.println("Elements of the map: "); for (String unKey : workers.keySet()) { System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey)); } String frstKey = workers.firstKey(); // accessing first key String lstKey = workers.lastKey(); // accessing last key System.out.println("Accessing name of first key in Map: " + frstKey); System.out.println("Accessing name of first key in Map: " + lstKey); } }
Output
Elements of the map: Name: Aman, Salary: 2000 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500 Name: Vaibhav, Salary: 4000 Name: Vivek, Salary: 1500 Accessing name of first key in Map: Aman Accessing name of first key in Map: Vivek
Similarities between TreeMap and TreeSet
By default, their elements are sorted by natural ordering. For example, they store strings in dictionary order and numerics in numerical order.
Since the elements are already sorted the access and retrieval time becomes faster. Because of this excellent feature, TreeMap and TreeSet are frequently used to store large amounts of information that need to be searched quickly.
Null values are not allowed.
They are defined inside ‘java.util’ package.
Both support Comparable Interface that can be implemented to define a custom sorting order.
Conclusion
In this article, we learned Map and Set Interface of the Collection Framework. Also, we discovered TreeMap and TreeSet classes that are used to implement the above interfaces. In the end, we discussed a few points that explain the similarities between these classes.