This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Previously we have discussed about
What is HashTable In Java ?
What is HashTable In Java ?
Here, we learn What is TreeMap In Java.But before starting we must know what is Map Interface In Java.? TreeMap is a implementation class of Map Interface.It belongs to java.util.*; package .
Class TreeMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>, NavigableMap<K,V>, SortedMap<K,V>
Declaration of TreeMap:
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, Serializable
Treemap sort according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
Treemap provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.
Sorted map performs all key comparisons using its compareTo (or compare) method.
Treemap implementation is not synchronized.If multiple threads access a map concurrently, and at least one of the threads modifies the map means that thread can adds or deletes one or more mappings.To prevent this we can synchronized the Map using
SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
Treemap is an example of fail-fast.And This class is a member of the Java Collections Framework.
Constructor and Description :-
TreeMap()
Constructs a new, empty tree map, using the natural ordering of its keys.
TreeMap(Comparator<? super K> comparator)
Constructs a new, empty tree map, ordered according to the given comparator.
TreeMap(Map<? extends K,? extends V> m)
Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys.
TreeMap(SortedMap<K,? extends V> m)
Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map.

No comments:
Post a Comment