Maps differ from lists and sets in that they are ordered collections of key-value pairs. : Map « Utility Classes « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Utility Classes » Map 
8.20.3.Maps differ from lists and sets in that they are ordered collections of key-value pairs.
The TreeMap class implements a sorted binary tree.

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;

public class MainClass {
  public static void main(String args[]) {
    TreeMap map = new TreeMap();
    map.put("one""1");
    map.put("two""2");
    map.put("three""3");
    map.put("four""4");
    map.put("five""5");
    map.put("six""6");
    displayMap(map);
  }

  static void displayMap(TreeMap map) {
    System.out.println("The size of the map is: " + map.size());
    Collection c = map.entrySet();
    Iterator i = c.iterator();
    while (i.hasNext()) {
      Object o = i.next();
      if (o == null)
        System.out.println("null");
      else
        System.out.println(o.toString());
    }
  }
}
The size of the map is: 6
five=5
four=4
one=1
six=6
three=3
two=2
8.20.Map
8.20.1.Create a Hashtable and stores a Uniform Resource Locator (URL) String with a short String as a key
8.20.2.HashMap extends AbstractMap and is similar to Hashtable.
8.20.3.Maps differ from lists and sets in that they are ordered collections of key-value pairs.
8.20.4.Object put(Object key, Object value) Associates key and value.
8.20.5.Object get(Object key) Returns the value associated with key, or null if no value is associated with key.
8.20.6.boolean containsKey(Object key) Returns true if the Map associates some value with key.
8.20.7.void clear() Empties the Map.
8.20.8.Set keySet() Returns a Set containing the Map's keys.
8.20.9.Collection values() Returns a collection containing the Map's values.
8.20.10.A Hash Map's keys are iterated in unpredictable order.
8.20.11.A Tree Map's keys are iterated in natural order.
8.20.12.TreeMap class implements the java.util.SortedMap interface, which extends java.util.Map.
8.20.13.SortedMap: Object first() Returns this set's first key.
8.20.14.Object lastKey() Returns this set's last key.
8.20.15.SortedMap headMap(Object toKey) Returns a view of the portion of this sorted map whose keys are strictly less than toKey.
8.20.16.SortedMap tailMap(Object fromKey) Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.
8.20.17.SortedMap subMap(Object fromKey, Object toKey) Returns a view of the portion of this sorted map whose keys range from fromKey, inclusive, to toKey, exclusive.
8.20.18.Maps check for key uniqueness
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.