Showing posts with label map-interface. Show all posts
Showing posts with label map-interface. Show all posts

Friday, 27 May 2011

SortedMap interface methods (java)

The SortedMap interface is used by TreeMap and adds additional methods to reflect that a TreeMap is sorted.
ResultMethodDescription
comp = comparator() Returns Comparator used to compare keys. null if natural ordering used (eg, String).
obj = firstKey() Key of first (in sorted order) element.
obj = lastKey() Key of last (in sorted order) element.
smp = headMap(obj) Returns SortedMap of all elements less than obj.
smp = tailMap(obj) Returns SortedMap of all elements greater than or equal to obj.
smp = subMap(fromKey, toKey) Returns SortedMap of all elements greater than or equal to fromKey and less than toKey.

Map.Entry interface methods (java)

Each element is a map has a key and value. Each key-value pair is saved in a java.util.Map.Entry object. A set of these map entries can be obtained by calling a map's entrySet() method. Iterating over a map is done by iterating over this set.
Assume in the following table that me is a Map.Entry object.
ResultMethod Description
obj = me.getKey() Returns the key from the pair.
objme.getValue(key) Returns the value from the Map pair.
objme.setValue(val) This is an optional operation and may not be supported by all Map.Entry objects. Sets the value of the pair, which modifies the Map which it belongs to. Returns the orginal value.