【问题标题】:java map iteration and comparisonjava map迭代和比较
【发布时间】:2015-01-07 07:23:37
【问题描述】:

如何将字符串与映射中键的值进行比较?

HashMap<cruise,String> passengers;


public int numPeopleOnCruise(int cruiseNumber, String name) {
    HashMap<cruise,String> pass=cruise.getPassengers();
    Cruise cruise=getCruise(cruiseNumber);      
    for (String s:pass) {
        if (name.equals(s))
            count++;
    } return count;
}

【问题讨论】:

标签: java map iterator comparison


【解决方案1】:

您应该遍历映射的条目集,然后访问列表中的值:

您应该在此处找到执行该操作的代码。

Iterate through a HashMap.

使用该帖子上的代码的想法:

public static void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
        if(pairs.getValue() == "something"){
            //do something
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 2017-05-02
    • 2021-05-03
    • 2020-01-26
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多