Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- public class MapSimples {
- public static void main(String[] args) {
- Map<Object, String> mapAnimais = new HashMap<Object, String>();
- //add os elementos no map por pares de chave-valor
- mapAnimais.put(new Integer(2), "Coelho");
- mapAnimais.put(new Integer(1), "Bode");
- mapAnimais.put(new Integer(3), "Cachorro");
- mapAnimais.put(new Integer(5), "Gato");
- mapAnimais.put(new Integer(4), "Vaca");
- // pegar o map pela interface Set para obter a chave e o valor
- Set s = mapAnimais.entrySet(); //set ja tem o sort
- //percorrer pelos pares(chave e valor) do Iterator pelo Map
- Iterator it = s.iterator();
- while(it.hasNext()) {
- Map.Entry m = (Map.Entry) it.next();
- int key = (Integer) m.getKey();
- String value = (String) m.getValue();
- System.out.println("Key: "+key+" Value: "+value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement