ejemplos de uso

class HashMap

  Map<String, String> mapa =

      new HashMap<String, String>();

 

  mapa.put("uno", "one");

  mapa.put("dos", "two");

  mapa.put("tres", "three");

  mapa.put("cuatro", "four");

  mapa.put("tres", "33");

  System.out.println(mapa.size());

  for (String clave: mapa.keySet()) {

      String valor = mapa.get(clave);

      System.out.println(clave + " -> " + valor);

  }

 

 

 

 

 

 

 

 

4

cuatro -> four

tres -> 33

uno -> one

dos -> two

class TreeMap

Map<String, String> mapa =

      new TreeMap<String, String>();

 

  mapa.put("uno", "one");

  mapa.put("dos", "two");

  mapa.put("tres", "three");

  mapa.put("cuatro", "four");

  mapa.put("tres", "33");

  System.out.println(mapa.size());

  for (String clave: mapa.keySet()) {

      String valor = mapa.get(clave);

      System.out.println(clave + " -> " + valor);

  }

 

 

 

 

 

 

 

 

4

cuatro -> four

dos -> two

tres -> 33

uno -> one

class LinkedHashMap

Map<String, String> mapa =

      new LinkedHashMap<String, String>();

 

  mapa.put("uno", "one");

  mapa.put("dos", "two");

  mapa.put("tres", "three");

  mapa.put("cuatro", "four");

  mapa.put("tres", "33");

  System.out.println(mapa.size());

  for (String clave: mapa.keySet()) {

      String valor = mapa.get(clave);

      System.out.println(clave + " -> " + valor);

  }

 

 

 

 

 

 

 

 

4

uno -> one

dos -> two

tres -> 33

cuatro -> four

 

Temas relacionados

24. Map<K, V> (interface) java.util.Map<K, V>