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;
}
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.
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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.