Java TreeMap
Java TreeMap
← prev next →
Java Collections
Collection Framework
Java ArrayList
Java LinkedList
ArrayList vs LinkedList
Java TreeSet The java.util package contains the Java TreeMap class, which is a
component of the Java Collections Framework. It extends the
Queue & PriorityQueue
AbstractMap class and implements the NavigableMap interface. TreeMap
Deque & ArrayDeque is an effective red-black tree-based solution that sorts key-value pairs.
Java Map Interface TreeMap works well in situations where ordered key-value pairs are
necessary since it preserves ascending order. It also only includes distinct
Java HashMap
components, guaranteeing that every key corresponds to a single value.
Working of HashMap
TreeMap can have more than one null value, even if it cannot have a null
Java LinkedHashMap key.
https://www.javatpoint.com/java-treemap 1/26
28/11/2024, 12:46 Java TreeMap
Java TreeMap
Java Hashtable
HashMap vs Hashtable
Java EnumSet
Java EnumMap
Collections class
Sorting Collections
Comparable interface
Comparator interface The important points about Java TreeMap class are:
Comparable vs Comparator
Java TreeMap contains values based on the key. It implements
Properties class the NavigableMap interface and extends AbstractMap class.
ArrayList vs Vector
Java TreeMap contains only unique elements.
Java Vector
Java TreeMap cannot have a null key but can have multiple null
Java Stack values.
Java Collection Interface
Java TreeMap is non synchronized.
Java Iterator Interface
Java TreeMap maintains ascending order.
Java Deque Interface
Java ConcurrentHashMap
TreeMap Class Declaration
Collection Quiz-1
Let's see the declaration for java.util.TreeMap class.
https://www.javatpoint.com/java-treemap 2/26
28/11/2024, 12:46 Java TreeMap
Java
ConcurrentHashMap
Java ConcurrentHashMap
hashcode() Method
Java ConcurrentHashMap
clear() Method
Java ConcurrentHashMap
computeIfAbsent() Method
Java ConcurrentHashMap
computeIfPresent() Method
public class TreeMap<k ,v> extends AbstractMap<k ,v> implement
Java ConcurrentHashMap s NavigableMap<k ,v>, Cloneable, Serializable
compute() Method
</k></k></k>
Java ConcurrentHashMap
containsKey() Method
Java ConcurrentHashMap
entrySet() Method
https://www.javatpoint.com/java-treemap 3/26
28/11/2024, 12:46 Java TreeMap
Method Description
Java
ConcurrentLinkedQueue Map.Entry<K,V> ceilingEntry(K It returns the key-value pair
key) having the least key, greater
Java ConcurrentLinkedQueue
than or equal to the specified
key, or null if there is no such
Java ConcurrentLinkedQueue
key.
toArray() Method
https://www.javatpoint.com/java-treemap 6/26
28/11/2024, 12:46 Java TreeMap
https://www.javatpoint.com/java-treemap 8/26
28/11/2024, 12:46 Java TreeMap
https://www.javatpoint.com/java-treemap 9/26
28/11/2024, 12:46 Java TreeMap
import java.util.*;
class TreeMap1{
https://www.javatpoint.com/java-treemap 10/26
28/11/2024, 12:46 Java TreeMap
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
</integer></integer>
Output:
100 Amit
101 Vijay
102 Ravi
103 Rahul
Explanation
This Java code illustrates how to store key-value pairs and loop through
the elements of a TreeMap class. Initially, the java.util.* package is
imported in order to use the TreeMap class and other associated utilities.
https://www.javatpoint.com/java-treemap 11/26
28/11/2024, 12:46 Java TreeMap
Next, a TreeMap instance map with integer keys and string values is
generated within the TreeMap1 class. Using the put() method, four key-
value pairs are added to the TreeMap. Every entry comprises a string value
that corresponds to an integer key.
import java.util.*;
https://www.javatpoint.com/java-treemap 12/26
28/11/2024, 12:46 Java TreeMap
Output:
https://www.javatpoint.com/java-treemap 13/26
28/11/2024, 12:46 Java TreeMap
101 Vijay
102 Ravi
103 Rahul
After invoking remove() method
100 Amit
101 Vijay
103 Rahul
Explanation
The Java code that is provided shows how to remove an entry using the
remove() method and stores key-value pairs using the TreeMap class. The
TreeMap is first updated with key-value pairs, which link integer keys to
matching string values.
Then, before and after using the remove() method to remove an entry
with the key 102, the contents of the TreeMap are reported. This is a brief
code piece that demonstrates the basic capability of adding, removing,
and iterating over entries in a TreeMap, highlighting its ordered and
mutable properties.
import java.util.*;
class TreeMap3{
public static void main(String args[]){
https://www.javatpoint.com/java-treemap 14/26
28/11/2024, 12:46 Java TreeMap
</integer></integer>
Output:
https://www.javatpoint.com/java-treemap 15/26
28/11/2024, 12:46 Java TreeMap
Explanation
This Java code creates a map that retains key-value pairs in ascending
order according to the keys by using the TreeMap class and the
NavigableMap interface. Following the addition of integer keys and
associated string values to the TreeMap, the code illustrates the several
navigation functions offered by the NavigableMap interface.
https://www.javatpoint.com/java-treemap 16/26
28/11/2024, 12:46 Java TreeMap
import java.util.*;
class TreeMap4{
public static void main(String args[]){
SortedMap<integer ,string> map=new TreeMap<integer ,string>
();
map.put(100,"Amit");
map.put(102,"Ravi");
map.put(101,"Vijay");
map.put(103,"Rahul");
//Returns key-
value pairs whose keys are less than the specified key.
https://www.javatpoint.com/java-treemap 17/26
28/11/2024, 12:46 Java TreeMap
System.out.println("headMap: "+map.headMap(102));
//Returns key-
value pairs whose keys are greater than or equal to the specified ke
y.
System.out.println("tailMap: "+map.tailMap(102));
//Returns key-value pairs exists in between the specified key.
System.out.println("subMap: "+map.subMap(100, 102));
}
}
</integer></integer>
Output:
Explanation
https://www.javatpoint.com/java-treemap 18/26
28/11/2024, 12:46 Java TreeMap
The SortedMap interface and TreeMap class are used in this Java code to
construct a sorted map whose key-value pairs are arranged according to
the keys' natural ordering, which in this case is integers. The code uses
methods given by the SortedMap interface to obtain subsets of the map
based on specified key ranges after initialising the TreeMap with integer
keys and matching string values.
HashMap TreeMap
https://www.javatpoint.com/java-treemap 19/26
28/11/2024, 12:46 Java TreeMap
HashMap allows null values, and TreeMap allows null values but
multiple null values can be not null keys.
associated with different keys.
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int
quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class MapExample {
public static void main(String[] args) {
//Creating map of Books
Map<integer ,book> map=new TreeMap<integer ,book>();
//Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Fo
rouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
//Adding Books to map
map.put(2,b2);
https://www.javatpoint.com/java-treemap 21/26
28/11/2024, 12:46 Java TreeMap
map.put(1,b1);
map.put(3,b3);
//Traversing map
for(Map.Entry<integer , book> entry:map.entrySet()){
int key=entry.getKey();
Book b=entry.getValue();
System.out.println(key+" Details:");
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher
+" "+b.quantity);
}
}
}
</integer></integer></integer>
Output:
1 Details:
101 Let us C Yashwant Kanetkar BPB 8
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
3 Details:
103 Operating System Galvin Wiley 6
Explanation
https://www.javatpoint.com/java-treemap 22/26
28/11/2024, 12:46 Java TreeMap
This Java code illustrates how to store and retrieve objects of a custom
class called Book using a TreeMap. Initially, the Book class is defined with a
constructor to initialise the attributes id, name, author, publisher, and
quantity.
https://www.javatpoint.com/java-treemap 23/26
28/11/2024, 12:46 Java TreeMap
← prev next →
Latest Courses
https://www.javatpoint.com/java-treemap 24/26
28/11/2024, 12:46 Java TreeMap
https://www.javatpoint.com/java-treemap 25/26
28/11/2024, 12:46 Java TreeMap
https://www.javatpoint.com/java-treemap 26/26