Skip to content

Instantly share code, notes, and snippets.

@preslavrachev
Created September 14, 2011 20:14
Show Gist options
  • Save preslavrachev/1217650 to your computer and use it in GitHub Desktop.
Save preslavrachev/1217650 to your computer and use it in GitHub Desktop.
Iterate through HashMap
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}
@HiroNakamura
Copy link

Funciona bien tu método. Your method works well.

import java.util.*;
public class Prueba{

//your method works well
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}

public static void main(String ... args)throws Exception{
HashMap<Integer,String> mapa=new HashMap<Integer,String>();

if(args.length!=0){
for(int i=0;i<args.length;i++){
mapa.put(i,args[i]);
}
printMap(mapa);
}else{
System.out.println("nada");
}

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment