Guest User

Untitled

a guest
Jun 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.HashMap;
  3.  
  4. public class CompositeMapsExample {
  5. public static void main(String[] args) {
  6. // Old and busted
  7. Map<String, Map<String, Object>> compositeMap = new HashMap<String, Map<String, Object>>();
  8. compositeMap.put("Jones", new HashMap<String, Object>());
  9. compositeMap.get("Jones").put("Jim", 5);
  10.  
  11. System.out.println(compositeMap);
  12.  
  13. // New hotness
  14. Map<Name, Object> compositeKey = new HashMap<Name, Object>();
  15. compositeKey.put(new Name("Jones", "Jim"), 5);
  16.  
  17. System.out.println(compositeKey);
  18. }
  19. }
  20.  
  21. class Name {
  22. public Name (String lastName, String firstName) {
  23. this.lastName = lastName;
  24. this.firstName = firstName;
  25. }
  26.  
  27. public boolean equals(Object o) {
  28. if (o == null || o.getClass() != this.getClass())
  29. return false;
  30. return ((Name) o).lastName.equals(this.lastName) &&
  31. ((Name) o).firstName.equals(this.firstName);
  32. }
  33.  
  34. public int hashCode() {
  35. return lastName.hashCode() + firstName.hashCode();
  36. }
  37.  
  38. public String toString() {
  39. return lastName + ", " + firstName;
  40. }
  41.  
  42. private final String lastName;
  43. private final String firstName;
  44. }
Add Comment
Please, Sign In to add comment