java hashmap key iteration

前端 未结 7 678
谎友^
谎友^ 2020-12-29 06:46

Is there any way to iterate through a java Hashmap and print out all the values for every key that is a part of the Hashmap?

7条回答
  •  感情败类
    2020-12-29 07:14

    Keep it simple, please:

    HashMap HeyHoLetsGo = new HashMap();
    
    HeyHoLetsGo.put("I", "wanna be your dog");
    HeyHoLetsGo.put("Sheena", "is a punk rocker");
    HeyHoLetsGo.put("Rudie", "can't fail");
    
    for ( String theKey : HeyHoLetsGo.keySet() ){
        System.out.println(theKey + " "+HeyHoLetsGo.get(theKey));
    }
    

提交回复
热议问题