Map.Entry: How to use it?

后端 未结 7 1572
梦毁少年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:24

    Map.Entry is a key and its value combined into one class. This allows you to iterate over Map.entrySet() instead of having to iterate over Map.keySet(), then getting the value for each key. A better way to write what you have is:

    for (Map.Entry entry : listbouton.entrySet())
    {
      String key = entry.getKey();
      JButton value = entry.getValue();
    
      this.add(value);
    }
    

    If this wasn't clear let me know and I'll amend my answer.

提交回复
热议问题