怎么从HashMap里面遍历值呢?
// 我这样去获取值拿到的是最后一个Key的值,哪里没写对? 怎么改呢?String strOutPutPort = this.outputPortComboBox.getSelectedItem().toString(); //从JComboBox里面获得值就是Key
Map<String, OutPutDataBean> outPutMap = chDevice.getRs232OutPut();
Set<String> strId = outPutMap.keySet();
Iterator it = strId.iterator();
while (it.hasNext()) {
String outPutID = (String) it.next();
if (outPutID.equals(strOutPutPort)) {
Collection<OutPutDataBean> outPutDatas = outPutMap.values();
for (OutPutDataBean outPutValue : outPutDatas) {
this.baudText.setText(outPutValue.getBaudRate());
this.dataBitText.setText(outPutValue.getDataBit());
this.parityText.setText(outPutValue.getParity());
this.stopText.setText(outPutValue.getStop());
}
}
}
// 我这样去获取值拿到的是最后一个Key的值,哪里没写对? 怎么改呢?String strOutPutPort = this.outputPortComboBox.getSelectedItem().toString(); //从JComboBox里面获得值就是Key
Map<String, OutPutDataBean> outPutMap = chDevice.getRs232OutPut();
Set<String> strId = outPutMap.keySet();
Iterator it = strId.iterator();
while (it.hasNext()) {
String outPutID = (String) it.next();
if (outPutID.equals(strOutPutPort)) {
Collection<OutPutDataBean> outPutDatas = outPutMap.values();
for (OutPutDataBean outPutValue : outPutDatas) {
this.baudText.setText(outPutValue.getBaudRate());
this.dataBitText.setText(outPutValue.getDataBit());
this.parityText.setText(outPutValue.getParity());
this.stopText.setText(outPutValue.getStop());
}
}
}
map.put("test", "test");
map.put("name", "mingjian");
Collection<String> collection = map.values();
for(Iterator<String> it = collection.iterator();it.hasNext();){
System.out.println(it.next());
}
import java.util.*;
public class MapSetTest {
public static void main(String[]args)
{
Map<String,String>staff=new HashMap<String,String>();
staff.put("1", "zhangsan");
staff.put("2", "lisi");
for(Map.Entry<String,String> entry:staff.entrySet())
{
String key=entry.getKey();
String value=entry.getValue();
System.out.println("key="+key+",value="+value);
}
}}
Map<String, String> map = new HashMap<String, String>();
map.put("test", "test");
map.put("name", "mingjian");
Set<Entry<String, String>> set = map.entrySet();
for (Entry<String, String> entry : set) {
System.out.println("Key:"+entry.getKey()+"\tValue:"+entry.getValue());
}
我想要1 对应的value,就拿它下面的value打印出来
我选中2 就拿2的value;
String key="name"; //假设这个key是你选中的值
Map<String, String> map = new HashMap<String, String>();
map.put("test", "test");
map.put("name", "mingjian");
Set<Entry<String, String>> set = map.entrySet();
for (Entry<String, String> entry : set) {
if(key.equals(entry.getKey())){
System.out.println("Key:"+entry.getKey()+"\tValue:"+entry.getValue());
break; //取到了直接跳出循环
}
}
也还是拿不到对应的value.是把
因为我拿到的永远是最后一个value.