Play audio slide show -- Keyboard shortcut: 'x'  Lecture overview -- Keyboard shortcut: 'u'  Previous page: Klasserne HashMap og TreeMap -- Keyboard shortcut: 'p'  Next page: Iterators for collections -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  Annotated slide -- Keyboard shortcut: 't'  Alphabetic index  Help page about these notes  Course home  Play sound for this slide -- Keyboard shortcut: 'y'    Collections og streams - slide 16 : 35

Et eksempel på anvendelse af Map 

import java.util.*;

public class Freq {
  private static final Integer ONE = new Integer(1);

  private static Integer incr(Integer i){
    return new Integer(i.intValue() + 1);
  }

  public static void main(String args[]) {
   Map m = new HashMap();
   // Initialize frequency table from command line
   for (int i=0; i<args.length; i++) {
       Integer freq = (Integer) m.get(args[i]);
       m.put(args[i], freq==null ? ONE : incr(freq));
     }
   System.out.println(m.size()+" distinct words detected:");
   System.out.println(m);
 }
}
 

Ved at udskifte "HashMap" med "TreeMap" vil der blive lavet en sorteret tabel