It is NavigatableMap implemetnation based on Re-Black tree concept.
It map is sorted according to the natural ordering of its keys, or by a Comparator provided at the time of creation.
Note that this implementation is not synchronized.
package collection.demos;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapDemo1 {
public static void main(String[] args) {
TreeMap<String, Long> phoneNumbers = new TreeMap<String, Long>();
//Putting values in TreeMap
phoneNumbers.put("Abc", 12345674L);
phoneNumbers.put("xyz", 453453344L);
phoneNumbers.put("Shoye", 23423433L);
phoneNumbers.put("Jusd", 656565653L);
phoneNumbers.put("Korp", 775554343L);
System.out.println("Values : " + phoneNumbers);
//Returning the first (lowest) key currently in this map.
System.out.println("First Key : " + phoneNumbers.firstKey());
//Returning the last (highest) key currently in this map.
System.out.println("Last Key : " + phoneNumbers.lastKey());
//Getting a key-value mapping associated with the least key in this map, or null if the map is empty.
Map.Entry<String, Long> entry = phoneNumbers.firstEntry();
System.out.println("firstEntry " + entry);
//Getting a key-value mapping associated with the greatest key in this map, or null if the map is empty.
entry = phoneNumbers.lastEntry();
System.out.println("Last Entry " + entry);
//Getting a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
entry = phoneNumbers.lowerEntry("Shoye");//Similler for lowerKey(K key) to get lower key
System.out.println("lowerEntry of Shoye key : " + entry);
//getting a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.
entry = phoneNumbers.ceilingEntry("Abc");//Similler for lowerKey(K key) to get lower key
System.out.println("ceilingEntry of Abc key : " + entry);
//Getting a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
NavigableMap<String, Long> headMap = phoneNumbers.headMap("Korp", true);
System.out.println("Head Map " + headMap);
//Getting a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
NavigableMap<String, Long> tailMap = phoneNumbers.tailMap("Korp", true);
System.out.println("Tail Map " + tailMap);
//rest of fucntionalities are simillar to Map
}
}
It map is sorted according to the natural ordering of its keys, or by a Comparator provided at the time of creation.
Note that this implementation is not synchronized.
package collection.demos;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapDemo1 {
public static void main(String[] args) {
TreeMap<String, Long> phoneNumbers = new TreeMap<String, Long>();
//Putting values in TreeMap
phoneNumbers.put("Abc", 12345674L);
phoneNumbers.put("xyz", 453453344L);
phoneNumbers.put("Shoye", 23423433L);
phoneNumbers.put("Jusd", 656565653L);
phoneNumbers.put("Korp", 775554343L);
System.out.println("Values : " + phoneNumbers);
//Returning the first (lowest) key currently in this map.
System.out.println("First Key : " + phoneNumbers.firstKey());
//Returning the last (highest) key currently in this map.
System.out.println("Last Key : " + phoneNumbers.lastKey());
//Getting a key-value mapping associated with the least key in this map, or null if the map is empty.
Map.Entry<String, Long> entry = phoneNumbers.firstEntry();
System.out.println("firstEntry " + entry);
//Getting a key-value mapping associated with the greatest key in this map, or null if the map is empty.
entry = phoneNumbers.lastEntry();
System.out.println("Last Entry " + entry);
//Getting a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
entry = phoneNumbers.lowerEntry("Shoye");//Similler for lowerKey(K key) to get lower key
System.out.println("lowerEntry of Shoye key : " + entry);
//getting a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.
entry = phoneNumbers.ceilingEntry("Abc");//Similler for lowerKey(K key) to get lower key
System.out.println("ceilingEntry of Abc key : " + entry);
//Getting a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
NavigableMap<String, Long> headMap = phoneNumbers.headMap("Korp", true);
System.out.println("Head Map " + headMap);
//Getting a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
NavigableMap<String, Long> tailMap = phoneNumbers.tailMap("Korp", true);
System.out.println("Tail Map " + tailMap);
//rest of fucntionalities are simillar to Map
}
}
Comments