Java 地图条目:如何使用它?

Java 地图条目:如何使用它?,java,dictionary,collections,hashmap,Java,Dictionary,Collections,Hashmap,我正在做一个计算器。 我把我的按钮放在一个HashMap集合中,当我想将它们添加到扩展了JPanel的类中时,我不知道如何从集合中获取按钮。 所以我在网上找到了我代码的最后两行,但我不知道它们的意思 这是我的密码: import java.awt.Component; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.swin

我正在做一个计算器。 我把我的按钮放在一个
HashMap
集合中,当我想将它们添加到扩展了
JPanel
的类中时,我不知道如何从集合中获取按钮。 所以我在网上找到了我代码的最后两行,但我不知道它们的意思

这是我的密码:

import java.awt.Component;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JPanel;


public class PanneauCalcul extends JPanel {

    private HashMap<String, JButton> listbouton = new HashMap<String, JButton>() ;

    public PanneauCalcul() {
        for(int i = 0; i < 10; i ++) {
            listbouton.put("num" + i, new JButton("" + i)) ;
        }

        listbouton.put("add", new JButton("+")) ;
        listbouton.put("soustract", new JButton("-")) ;
        listbouton.put("multiply", new JButton("x")) ;
        listbouton.put("divise", new JButton("/")) ;
        listbouton.put("equal", new JButton("=")) ;

        Set entrys = listbouton.entrySet() ;

        Iterator iter = entrys.iterator() ;

        while(iter.hasNext()) {
            Map.Entry me = (Map.Entry)iter.next();  //don't understand 
            this.add((Component) me.getValue()) ;   //don't understand
        }

        EcouteCalcul ecout = new EcouteCalcul(this) ;
    }
}
导入java.awt.Component;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入java.util.Set;
导入javax.swing.JButton;
导入javax.swing.JPanel;
公共类PanneauCalcul扩展JPanel{
private HashMap listbooton=new HashMap();
公共PanneauCalcul(){
对于(int i=0;i<10;i++){
put(“num”+i,newjbutton(“+i));
}
listbouton.put(“添加”,新的JButton(“+”));
listbouton.put(“soustract”,新的JButton(“-”);
放置(“乘法”,新的JButton(“x”);
listbouton.put(“除数”,新的JButton(“/”);
listbouton.put(“相等”,新的JButton(“=”);
Set entrys=listbooton.entrySet();
迭代器iter=entrys.Iterator();
while(iter.hasNext()){
Map.Entry me=(Map.Entry)iter.next();//不明白
this.add((组件)me.getValue());//不明白
}
EcouteCalcul ecout=新的EcouteCalcul(本);
}
}

我不明白我们如何使用
Map.Entry
——这是一个界面——而不重新定义
Map.Entry
的函数。

这段代码最好重写为:

for( Map.Entry me : entrys.entrySet() )
{
    this.add( (Component) me.getValue() );
}
这相当于:

for( Component comp : entrys.getValues() )
{
    this.add( comp );
}
当您枚举映射的条目时,迭代会产生一系列对象,这些对象实现了
map.Entry
接口。这些对象中的每一个都包含一个键和一个值


枚举映射的条目比枚举其值要稍微有效一些,但是这个factoid假定您的
map
HashMap
,并且还假定了解
HashMap
类的内部工作(实现细节)。可以更确定地说,无论map是如何实现的(无论它是
HashMap
还是其他什么),如果您需要map的键和值,然后,枚举条目将比枚举键更有效,然后对于每个键,再次调用映射以查找相应的值。

map。Entry
是一个键及其值组合到一个类中。这允许您迭代
Map.entrySet()
,而不必迭代
Map.keySet()
,然后获取每个键的值。写下你所拥有的东西的更好方法是:

for (Map.Entry<String, JButton> entry : listbouton.entrySet())
{
  String key = entry.getKey();
  JButton value = entry.getValue();

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

如果不清楚,请告诉我,我会修改我的答案。

映射是键+值对的集合,其可视化方式如下:

{[fooKey=fooValue],barKey=barValue],[quxKey=quxValue]}
地图界面允许访问此集合的几个选项:键集
[fooKey,barKey,quxKey]
,值集
[fooValue,barValue,quxValue]
,最后是输入集
[fooKey=fooValue],barKey=barValue],[quxKey=quxValue]

条目集只是一种在映射中迭代键值对的便利,映射。条目是每个键值对的表示。执行最后一个循环的等效方法是:

for (String buttonKey: listbouton.keySet()) {
    this.add(listbouton.get(buttonKey)) ;
}


映射由键/值对组成。例如,在代码中,一个键是“Add”,关联的值是JButton(“+”)。映射项是映射中包含的单个键/值对。最常用的两种方法是
getKey()和getValue()
。您的代码将所有对放入一个集合:

Set entrys = listbouton.entrySet() ;
并对它们进行迭代。现在,它只使用
me.getValue()
查看值部分,并将它们添加到您的PanneauCalcul中

this.add((Component) me.getValue()) ;   //don't understand
如果需要同时查看键和值,这种类型的循环(在Map.Entry上)通常是有意义的。但是,在您的情况下,您没有使用键,因此一个简单得多的版本是只获取地图中的所有值并添加它们。e、 g

for (JButton jb:listbouton.values()) {
  this.add(jb);
}
最后一点意见。HashMap中的迭代顺序是非常随机的。因此,按钮将以半随机顺序添加到您的PanneauCalcul中。如果要保留按钮的顺序,应使用LinkedHashMap。

哈希映射将(键、值)对存储为Map.Entry类型。如您所知,哈希映射使用链接哈希映射(以防发生冲突)。因此,哈希映射的Bucket中的每个节点都是Map.Entry类型。因此,无论何时遍历哈希映射,都会得到Map.Entry类型的节点


现在,在您的示例中,当您遍历哈希映射时,您将获得Map.Entry Type(即接口),以从该映射中获取键和值。Entry节点对象、接口提供的方法,如getValue()、getKey()等。因此,根据代码,在您的对象中添加所有运算符jbutton,即(+、-、/、*、=)

Map.Entry接口帮助我们迭代Map类

检查以下简单示例:

public class MapDemo {
    public static void main(String[] args) {
     Map<Integer,String> 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());
        }
    }
}
公共类MapDemo{
公共静态void main(字符串[]args){
Map Map=newhashmap();
地图放置(1,“卡姆兰”);
地图放置(2,“阿里”);
地图放置(3,“从”);
图.put(4,“Dir”);
地图放置(5,“较低”);
对于(Map.Entry m:Map.entrySet()){
System.out.println(m.getKey()+“”+m.getValue());
}
}
}

注意,您也可以使用Map.Entry作为主类型,使用其基本实现AbstractMap.SimpleEntry创建自己的结构。例如,如果希望有一个有序的条目列表,可以编写:

List<Map.Entry<String, Integer>> entries = new ArrayList<>();
entries.add(new AbstractMap.SimpleEntry<String, Integer>(myStringValue, myIntValue));
List entries=new ArrayList();
add(newAbstractMap.SimpleEntry(myStringValue,myIntValue));
等等。从…起
List<Map.Entry<String, Integer>> entries = new ArrayList<>();
entries.add(new AbstractMap.SimpleEntry<String, Integer>(myStringValue, myIntValue));