|
Example:
|
|
|
|
|
java.util Interface Map<K,V> Type Parameters: K - the type of keys maintained by this map V - the type of mapped values All Known Subinterfaces: Bindings, ConcurrentMap<K,V>, ConcurrentNavigableMap<K,V>, LogicalMessageContext, MessageContext, NavigableMap<K,V>, SOAPMessageContext, SortedMap<K,V> All Known Implementing Classes: AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap |
|
(Count the number of items in a command argument)
public static void main(String[] args) { int i; Integer freq; String s; Map<String, Integer> M = new HashMap<String, Integer>(); // Use HashMap implementation of Map /* ------------------------------------------------------- Enter arg strings into the Map along with frequency ------------------------------------------------------- */ for (i = 0; i < args.length; i++) { s = args[i]; freq = M.get(s); if ( freq == null ) { M.put(s, 1); } else { M.put(s, freq.intValue() + 1); } System.out.println("After processing `" + args[i] + "':"); System.out.println(M); System.out.println(); } |
Example Program: (Demo above code)                                                
(Iterating through keys)
public static void main(String[] args) { Map<String, String> M = new HashMap<String, String>(); M.put("John Doe", "Brother"); M.put("Tom Doe", "Cousin"); M.put("Jane Doe", "Sister"); M.put("Todd Hall", "Neighbor"); M.put("Ralph Smith", "Teacher"); Collection<String> s = M.keySet(); /* ------------------------------------------------ Get an iterator over a Set ----------------------------------------------- */ Iterator iter = s.iterator(); /* ------------------------- Iterate over "iter" ------------------------- */ while(iter.hasNext()) { String x; // Help variable x = (String) iter.next(); // Get next entry System.out.println(x); } System.out.println(); } |
Example Program: (Demo above code)                                                
(The entrySet is a compound data structure)
public static void main(String[] args) { Map<String, String> M = new HashMap<String, String>(); M.put("John Doe", "Brother"); M.put("Tom Doe", "Cousin"); M.put("Jane Doe", "Sister"); M.put("Todd Hall", "Neighbor"); M.put("Ralph Smith", "Teacher"); /* ------------------------------------------------ entrySet() returns a "Set<Map.Entry<K,V>>" or: a "Set" of |
Example Program: (Demo above code)                                                
(I.e., we will learn -- among other things --- how to write the HashMap class)
|