The implementation of a TreeMap is not synchronized. This means that if multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by using the Collections.synchronizedSortedSet method. This is best done at the creation time, to prevent accidental unsynchronized access to the set. This can be done as:
SortedMap m = Collections.synchronizedSortedMap(new TreeMap(…));
How Does the TreemMap work Internally?
The methods in a TreeMap while getting keyset and values, return an Iterator that are fail-fast in nature. Thus, any concurrent modification will throw ConcurrentModificationException. A TreeMap is based upon a redblack tree data structure. Each node in the tree has:
- 3 Variables (K key=Key, V value=Value, boolean color=Color)
- 3 References (Entry left = Left, Entry right = Right, Entry parent = Parent)
In order to create a TreeMap, we need to create an object of the TreeMap class. The TreeMap class consists of various constructors that allow the possible creation of the TreeMap. The following are the constructors available in this class:
1. TreeMap(): This constructor is used to build an empty treemap that will be sorted by using the natural order of its keys. Let’s understand this with an example:
// Java program to demonstrate an
// example of TreeMap using the
// default constructor
import
java.util.*;
import
java.util.concurrent.*;
public
class
TreeMapImplementation {
// Function to show TreeMap()
// constructor example
static
void
Example1stConstructor()
{
// Creating an empty TreeMap
TreeMap<Integer, String> tree_map
=
new
TreeMap<Integer, String>();
// Mapping string values to int keys
tree_map.put(
10
,
"Geeks"
);
tree_map.put(
15
,
"4"
);
tree_map.put(
20
,
"Geeks"
);
tree_map.put(
25
,
"Welcomes"
);
tree_map.put(
30
,
"You"
);
// Displaying the TreeMap
System.out.println(
"TreeMap: "
+ tree_map);
}
// Driver code
public
static
void
main(String[] args)
{
System.out.println(
"TreeMap using "
+
"TreeMap() constructor:\n"
);
Example1stConstructor();
}
}
Output:
TreeMap using TreeMap() constructor: TreeMap: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
2. TreeMap(Comparator comp): This constructor is used to build an empty TreeMap object in which the elements will need an external specification of the sorting order. Let’s understand this with an example:
// Java program to demonstrate
// an example of TreeMap using
// a comparator constructor
import
java.util.*;
import
java.util.concurrent.*;
// A class to represent a student.
class
Student {
int
rollno;
String name, address;
// Constructor
public
Student(
int
rollno, String name,
String address)
{
this
.rollno = rollno;
this
.name = name;
this
.address = address;
}
// Used to print student details
// in main()
public
String toString()
{
return
this
.rollno +
" "
+
this
.name +
" "
+
this
.address;
}
}
// Comparator implementattion
class
Sortbyroll
implements
Comparator<Student> {
// Used for sorting in ascending order of
// roll number
public
int
compare(Student a, Student b)
{
return
a.rollno - b.rollno;
}
}
public
class
TreeMapImplementation {
static
void
Example2ndConstructor()
{
// Creating an empty TreeMap
TreeMap<Student, Integer> tree_map
=
new
TreeMap<Student, Integer>(
new
Sortbyroll());
// Mapping string values to int keys
tree_map.put(
new
Student(
111
,
"bbbb"
,
"london"
),
2
);
tree_map.put(
new
Student(
131
,
"aaaa"
,
"nyc"
),
3
);
tree_map.put(
new
Student(
121
,
"cccc"
,
"jaipur"
),
1
);
// Displaying the TreeMap
System.out.println(
"TreeMap: "
+ tree_map);
}
public
static
void
main(String[] args)
{
System.out.println(
"TreeMap using "
+
"TreeMap(Comparator)"
+
" constructor:\n"
);
Example2ndConstructor();
}
}
Output:
TreeMap using TreeMap(Comparator) constructor: TreeMap: {111 bbbb london=2, 121 cccc jaipur=1, 131 aaaa nyc=3}
3. TreeMap(Map M): This constructor is used to initialize a TreeMap with the entries from the given map M which will be sorted by using the natural order of the keys. Let’s understand this with an example:
// Java program to demonstrate an
// example of TreeMap using the
// default constructor
import
java.util.*;
import
java.util.concurrent.*;
public
class
TreeMapImplementation {
static
void
Example3rdConstructor()
{
// Creating a Map
Map<Integer, String> hash_map
=
new
HashMap<Integer, String>();
// Mapping string values to int keys
hash_map.put(
10
,
"Geeks"
);
hash_map.put(
15
,
"4"
);
hash_map.put(
20
,
"Geeks"
);
hash_map.put(
25
,
"Welcomes"
);
hash_map.put(
30
,
"You"
);
// Creating the TreeMap using the Map
TreeMap<Integer, String> tree_map
=
new
TreeMap<Integer, String>(hash_map);
// Displaying the TreeMap
System.out.println(
"TreeMap: "
+ tree_map);
}
public
static
void
main(String[] args)
{
System.out.println(
"TreeMap using "
+
"TreeMap(Map)"
+
" constructor:\n"
);
Example3rdConstructor();
}
}
Output:
TreeMap using TreeMap(Map) constructor: TreeMap: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
4. TreeMap(SortedMap sm): This constructor is used to initialize a TreeMap with the entries from the given sorted map which will be stored in the same order as the given sorted map. Let’s understand this with an example:
// Java program to demonstrate an
// example of TreeMap using the
// sortedmap constructor
import
java.util.*;
import
java.util.concurrent.*;
public
class
TreeMapImplementation {
// Function to show
// TreeMap(SortedMap) constructor example
static
void
Example4thConstructor()
{
// Creating a SortedMap
SortedMap<Integer, String> sorted_map
=
new
ConcurrentSkipListMap<Integer, String>();
// Mapping string values to int keys
sorted_map.put(
10
,
"Geeks"
);
sorted_map.put(
15
,
"4"
);
sorted_map.put(
20
,
"Geeks"
);
sorted_map.put(
25
,
"Welcomes"
);
sorted_map.put(
30
,
"You"
);
// Creating the TreeMap using the SortedMap
TreeMap<Integer, String> tree_map
=
new
TreeMap<Integer, String>(sorted_map);
// Displaying the TreeMap
System.out.println(
"TreeMap: "
+ tree_map);
}
// Driver code
public
static
void
main(String[] args)
{
System.out.println(
"TreeMap using "
+
"TreeMap(SortedMap)"
+
" constructor:\n"
);
Example4thConstructor();
}
}
Output:
TreeMap using TreeMap(SortedMap) constructor: TreeMap: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}