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

解决方案 »

  1.   

    Map遍历值:Map<String,String> map = new HashMap<String,String>();
    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());
    }
      

  2.   

    我要根据相应的Key取到对应的Value,怎么取?
      

  3.   

    不就是map。get(相应的key);么。
      

  4.   


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

  5.   


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

  6.   

    还是有点问题,还是只能拿一个value,不是我选中的那个Key,就去获得相应的value
      

  7.   


      我想要1 对应的value,就拿它下面的value打印出来
    我选中2 就拿2的value;
      

  8.   


    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; //取到了直接跳出循环
        }
    }
      

  9.   

    这个得到的是对应的键(Key) outPutMap.keySet(); //键的集合
      

  10.   

    像你那样也可以,,你如果得到了键(key) 可以通过这样得到值(value) map.get(key);
      

  11.   

    这么说 我从这个集合里面去拿到对应的key跟我选中的值比较
    也还是拿不到对应的value.是把
    因为我拿到的永远是最后一个value.