Map.Entry: How to use it?

后端 未结 7 1575
梦毁少年i
梦毁少年i 2020-12-04 07:41

I\'m working on creating a calculator. I put my buttons in a HashMap collection and when I want to add them to my class, which extends JPanel, I do

7条回答
  •  星月不相逢
    2020-12-04 08:13

    Map.Entry interface helps us iterating a Map class

    Check this simple example:

    public class MapDemo {
        public static void main(String[] args) {
         Map map=new HashMap();
         map.put(1, "Kamran");
            map.put(2, "Ali");
            map.put(3, "From");
            map.put(4, "Dir");
            map.put(5, "Lower");
            for(Map.Entry m:map.entrySet()){
            System.out.println(m.getKey()+"  "+m.getValue());
            }
        }
    }
    

提交回复
热议问题