繁体   English   中英

Map.Entry:如何使用?

[英]Map.Entry: How to use it?

我正在创建一个计算器。 我将按钮放在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) ;
    }
}

我不明白我们如何在不重新定义Map.Entry的功能的情况下使用Map.Entry -- 这是一个接口。

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);
}

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

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

List<Map.Entry<String, Integer>> entries = new ArrayList<>();
entries.add(new AbstractMap.SimpleEntry<String, Integer>(myStringValue, myIntValue));

等等。 从那里,你有一个元组列表。 当您想要有序元组并且基本 Map 是不可行的时非常有用。

这段代码更好地重写为:

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

它相当于:

for( Component comp : entrys.getValues() )
{
    this.add( comp );
}

当您枚举地图的条目时,迭代会产生一系列实现Map.Entry接口的对象。 这些对象中的每一个都包含一个键和一个值。

枚举映射的条目应该比枚举其值更有效,但是这个事实假设您的MapHashMap ,并且还假设了解HashMap类的内部工作原理(实现细节)。 可以更肯定地说,无论你的映射是如何实现的(无论是HashMap还是其他什么),如果你同时需要映射的键和值,那么枚举条目将比枚举键然后为每个键再次调用映射以查找相应值更有效。

Hash-Map 将 (key,value) 对存储为 Map.Entry Type。如您所知,Hash-Map 使用 Linked Hash-Map(以防发生冲突)。 因此Hash-Map桶中的每个节点都是Map.Entry类型。 因此,每当您遍历 Hash-Map 时,您都会获得类型为 Map.Entry 的节点。

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

Map 是 Key + Value 对的集合,可视化如下:

{[fooKey=fooValue],barKey=barValue],[quxKey=quxValue]}

Map 接口允许访问此集合的几个选项:键集[fooKey, barKey,quxKey] ,值集[fooValue, barValue, quxValue]和最后的条目集[fooKey=fooValue],barKey=barValue],[quxKey=quxValue]

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

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

要么

for (JButton button: listbouton.values()) {
    this.add(button) ;
}

Map 由键/值对组成。 例如,在您的代码中,一个键是“Add”,关联的值是 JButton("+")。 Map.Entry 是包含在 Map 中的单个键/值对。 两个最常用的方法是getKey() and getValue() 您的代码将所有对组合成一个集合:

Set entrys = listbouton.entrySet() ;

并迭代它们。 现在,它只使用me.getValue()查看部分并将它们添加到您的 PanneauCalcul

this.add((Component) me.getValue()) ;   //don't understand

通常这种类型的循环(在Map.Entry的)是有道理的,如果你需要看的键和值两者 但是,在您的情况下,您没有使用键,因此更简单的版本是获取地图中的所有并添加它们。 例如

for (JButton jb:listbouton.values()) {
  this.add(jb);
}

最后一个评论。 HashMap 中的迭代顺序是非常随机的。 因此,按钮将以半随机顺序添加到您的 PanneauCalcul。 如果您想保留按钮的顺序,您应该使用 LinkedHashMap。

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());
        }
    }
}

 public HashMap<Integer,Obj> ListeObj= new HashMap<>(); public void addObj(String param1, String param2, String param3){ Obj newObj = new Obj(param1, param2, param3); this.ListObj.put(newObj.getId(), newObj); } public ArrayList<Integer> searchdObj (int idObj){ ArrayList<Integer> returnList = new ArrayList<>(); for (java.util.Map.Entry<Integer, Obj> e : this.ListObj.entrySet()){ if(e.getValue().getName().equals(idObj)) { returnList.add(e.getKey()); } } return returnList; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM