TreeMap in java
The TreeMap class is sorted according to the natural order means ascending order of its keys, or you can provide by implementing a Comparator interface provided at map creation
time, depending on which constructor is used.
Java TreeMap
is a part of collection since jdk 1.2 versions and available in java.util
package. TreeMap implements the Map interface and extends AbstractMap class. TreeMap
stores data in key & value pairs.
TreeMap - Here k means Key which is any data type
V means Value of any data types
A
Red-Black tree based on the NavigableMap interface implementation, This
provides guaranteed log(n) time
cost for the containsKey, get, put and remove operations.
TreeMap
class Declaration
public class TreeMap
extends
AbstractMap
implements
NavigableMap, Cloneable, java.io.Serializable
Features
of TreeMap class
1] TreeMap stores unique key only. Duplicate key not
allowed if we try to enter then recent one get override by previous one.
2] TreeMap does not
allow null key and if we try to add null key then at runtime throws java.lang.NullPointerException
Exaception.
3] TreeMap
does not maintain insertion order
because element are sorted based on the ascending order. So
if you are using any class as key, make sure it’s implementing Comparable
interface for natural ordering.
4] TreeMap
allows multiple null values but key
should not be null.
TreeMap
class implementation is not synchronized.
If multiple threads access a map
concurrently, and at least one of the threads modifies the map structurally,
it must be synchronized externally.
SortedMap m =
Collections.synchronizedSortedMap(new TreeMap(...));
5] The iterators returned by the iterator method of the collections returned by all of this class's "collection view methods" are fail-fast. If the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw exception named a ConcurrentModificationException.
Constructor
and Description
TreeMap<K, V> tm = new
TreeMap()
Constructs a new, empty tree map, using the natural ordering of
its keys.
TreeMap<K, V> tm = new
TreeMap(Comparator comparator)
Constructs a new, empty tree map, ordered according to the given
comparator.
TreeMap<K, V> tm = new
TreeMap(Map m)
Constructs a new tree map containing the same mappings as the
given map, ordered according to the natural ordering of its keys.
TreeMap<K, V> tm = new
TreeMap(SortedMap m)
Constructs a new tree map containing the same mappings and using
the same ordering as the specified sorted map.
Java TreeMap
Example
package com.vr.techfloaters;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap tm = new TreeMap<>();
//Adding elements
tm.put(50, "java");
tm.put(30, ".Net");
tm.put(20, "php");
tm.put(15, null);
tm.put(35, "python");
tm.put(10, null);
//Get values based
on key
System.out.println(tm.get(20));
//Remove key and
values based on key
System.out.println(tm.remove(10));
//Iterating the
element in HashMap
Set<
Entry > set = tm.entrySet();
Iterator> itr = set.iterator();
while(itr.hasNext()){
Entry entry = itr.next();
System.out.println(entry.getKey() +" : "+entry.getValue());
}
}
}
Output
php
null
15 : null
20 : php
30 : .Net
35 : python
50 : java
That’s all for a basic concept for TreeMap in java, I hope that you
enjoyed reading it and let us know in comment if anything you want.
0 comments:
Post a Comment