Tuesday, 11 October 2016

What is SortedMap In Java ?

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.
Here we learn what is SortedMap in java.But before starting this you must know What is Map Interface in Java ?
Interface SortedMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

All Superinterfaces:

All Known Subinterfaces:

All Known Implementing Classes:
ConcurrentSkipListMap, TreeMap

Declaration of SortedMap:
public interface SortedMap<K,V>
extends Map<K,V>

All keys inserted into a sorted map must implement the Comparable interface.
And all such keys must be mutually comparable: k1.compareTo(k2) (or comparator.compare(k1, k2)) must not throw a ClassCastException for any keys k1 and k2 in the sorted map.
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if the sorted map is to correctly implement the Map interface.
This is so because the Map interface is defined in terms of the equals operation, but a sorted map performs all key comparisons using its compareTo (or compare) method.

Sorted map implementation classes should provide four standard constructors :-

A void (no arguments) constructor, which creates an empty sorted map sorted according to the natural ordering of its keys.
A constructor with a single argument of type Comparator, which creates an empty sorted map sorted according to the specified comparator.
A constructor with a single argument of type Map, which creates a new map with the same key-value mappings as its argument, sorted according to the keys' natural ordering.
A constructor with a single argument of type SortedMap, which creates a new sorted map with the same key-value mappings and the same ordering as the input sorted map.

This interface is a member of the Java Collections Framework.

Method Summary of SortedMap:


Modifier and Type
Method Name
Description
Comparator<? super K>
comparator()
Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
Set<Map.Entry<K,V>>
entrySet()
Returns a Set view of the mappings contained in this map.
K
firstKey()
Returns the first (lowest) key currently in this map.
headMap(K toKey)
Returns a view of the portion of this map whose keys are strictly less than toKey.
Set<K>
keySet()
Returns a Set view of the keys contained in this map.
K
lastKey()
Returns the last (highest) key currently in this map.
subMap(K fromKey, K toKey)
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
tailMap(K fromKey)
Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
values()
Returns a Collection view of the values contained in this Map.


No comments:

Post a Comment