OO Programming with Java


Maps

  1. Maps are usefull to implements Dictionaries, Properties' lists, etc.

As an illustration, a simple translator can be defined as follow:

public static void main(String[] args) {
  String[] words = "this is a simple translator".split(" ");
  Map<String,String> dict = new HashMap<String,String>();
  dict.put("a", "un");
  dict.put("is", "est");
  dict.put("this", "ceci");
  dict.put("translator", "traducteur");
  for(String word : words) {
    if (dict.containsKey(word))
      word = dict.get(word);
    System.out.print(word+" ");
  } // ceci est un simple traducteur
}

11 - 13