Reputation: 289
I created an association list here:
HashMap<Integer, String> PhoneNumber = new HashMap<>();
PhoneNumber.put(8559966, "Jill");
PhoneNumber.put(6669999, "James");
PhoneNumber(255666, "Dylan");
What I want to do is call this list in a method but I want to only look at the String half, Jill, James and Dylan. I'm lost on how to do this. I know how to call an ArrayList into a method but not how to call a HashMap and how only look at the Strings.
What I want to do is something likes this:
public String getJill(ArrayList<Integer> PhoneNumber){
String h;
for(int i = 0; i<PhoneNumber.size();; i++){
if(PhoneNumber.values(i) == "Jill"){
h = "Jill";
}
return h;
}
}
Upvotes: 1
Views: 1295
Reputation: 9331
Do you mean Map.values()
which returns a Collection?
public static void main(String[] args) {
Map<Integer, String> phoneNumber = new HashMap<>();
phoneNumber.put(8559966, "Jill");
phoneNumber.put(6669999, "James");
phoneNumber.put(255666, "Dylan");
for(String s : phoneNumber.values()) {
System.out.println(s);
}
}
Output in my case:
Jill
Dylan
James
Now if you want them in the same order you put them in the Map
you must use LinkedHashMap
instead of HashMap
Map<Integer, String> phoneNumber = new LinkedHashMap<>();
Then your output will be:
Jill
James
Dylan
Upvotes: 3