Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap HashSet or ...

 
alan fisher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class PrintingContainers {
static Map mfill(Map m) {
m.put("dog", "Bosco");
m.put("dog", "Spot");
return m;
}
public static void main(String[] args) {
System.out.println(mfill(new HashMap()));
}
}
// output {dog=Spot}
From this program,can I deduce that HashMap can only hold the mapping from one key to one object?If it does,how can I hold a mapping from one key to some objects?
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maps contain a key, value pair.
If you want to key multiple objects just make the value a Collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic