[英]How to use Map.Entry interface correctly, and why are Map.Entry's methods implemented already?
[英]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
接口的對象。 這些對象中的每一個都包含一個鍵和一個值。
枚舉映射的條目應該比枚舉其值更有效,但是這個事實假設您的Map
是HashMap
,並且還假設了解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.