- The TreeMap is part of the Java Collections Framework.
- TreeMap is a concrete class which implements the Map interface, which maps keys to values.
- Keys are like the index of an ArrayList except that the index of an ArrayList is an int. A map key can be any object.
- A map cannot contain duplicate keys - each key maps to one value.
- A key and it's corresponding value form an entry or entry-set. Let's call them key/value pairs.
- A TreeMap is sorted by the natural ordering of it's keys.
Instantiation
Map<key,Value> = new TreeMap<Key,Value>();
(As map is an Interface, it cannot be instantiated.)
get(Object key)
Returns the value for the specified key. If a value at that key does not exist, null is returned. While this is most forgiving, always check the return value for null before attempting to use it.
Object o = myMap.get(x);
if (o != null) {...};
containsKey(int key)
Returns True if the specified key is found in the map. (False is not!).
if (myMap.containsKey(x)) {...};
put(Object key, Object value)
Puts the new value at the specified key. If the key already exists, the old value is replaced. So be careful!
myMap.put(x,o);
size()
Returns the number of key/value pairs.
myMap.size();
remove(int key)
Removes the value at the specified key. If no value as associated with the key, the method simply exits.
myMap.remove(x);
putAll(Map)
Copies all of the mappings from the specified map, to this map. Both maps must have the same key and value type. Beware though, if there are values for identical keys, the old value will be replaced with the new value - as per the *no duplicates* rule.
For instance:
Map<Integer, String> mapOne = new TreeMap<Integer,String>();
mapOne.put(10, "ten");
mapOne.put(3, "three");
Map<Integer,String> mapTwo = new TreeMap<Integer,String>();
mapTwo.put(50, "fifty");
mapTwo.put(10, "ten");
mapOne.putAll(mapTwo);
mapOne now contains the following key/value pairs (sorted):
- 3, "three"
- 10, "ten"
- 50, "fifty"
Iterating (looping) using Enhanced For Loop
The following methods are utilised in the enhanced for loop example below:
- entrySet() returns a view of the key/value pairs in the map.
- getKey() returns the key in the current iteration.
- getValue() returns the value of the key in the current iteration
In plain English, we are saying that for each Integer and String, key/value pair in the map.
for (Map.Entry<Integer, String> mapEntries : mapOne.entrySet()) {
int key = mapEntries.getKey();
String value = mapEntries.getValue();
System.out.println("Key is: " + key);
System.out.println("Value is: " + value);
}
We would expect the output to look like:
3
three
10
ten
50
fifty
Pulling keys or values out of Map and into ArrayList
In this example, a map of Integer,Integer is split into two lists - one for keys and one for values.
List<Integer> listOfMapKeys = new ArrayList<Integer>(mapname.keySet());
List<Integer> listOfMapValues = new ArrayList<Integer>(mapname.values());
But the coolest stuff is yet to come!!! Read on in NavigableMap Interface >>>
In this example, a map of Integer,Integer is split into two lists - one for keys and one for values.
List<Integer> listOfMapKeys = new ArrayList<Integer>(mapname.keySet());
List<Integer> listOfMapValues = new ArrayList<Integer>(mapname.values());
But the coolest stuff is yet to come!!! Read on in NavigableMap Interface >>>
(also, will post code download soon too!)
(also, IS ANYONE READING THIS?) :p
