0

How do I compare a string with the values of a key in a map?

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

1 Answers1

0

You should iterate through the entry set of the map and then access the values in the list:

You should find code to go through that here.

Iterate through a HashMap.

An idea using the code on that post:

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
        }
    }
}
Community
  • 1
  • 1
Zeratas
  • 1,005
  • 3
  • 17
  • 36