Guest User

Untitled

a guest
Mar 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. public qweqwe() {
  2. this.map = new HashMap<Person, List<Pet>>();
  3. }
  4.  
  5. Person [name=Yurii]
  6. Pet [nickname=Margo, vyd=Dog]
  7. Pet [nickname=Chita, vyd=Dog]
  8. Person [name=qweqwe]
  9. Pet [nickname=Chita, vyd=Dog]
  10.  
  11. void remove(Map<Person, List<Pet>> map, String name, String nickname) {
  12. Maps.filterKeys(map, person -> person.getName().equals(name)).forEach((person, pets) -> {
  13. pets.removeIf(pet -> pet.getName().equals(nickname));
  14. });
  15. }
  16.  
  17. void remove(Map<Person, List<Pet>> map, String name, String nickname) {
  18. map.entrySet().stream().filter(person -> person.getName().equals(name)).collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())).forEach((person, pets) -> {
  19. pets.removeIf(pet -> pet.getName().equals(nickname));
  20. });
  21. }
  22.  
  23. public class Person {
  24. private String name;
  25.  
  26. public Person(String name) {
  27. this.name = name;
  28. }
  29.  
  30. @Override
  31. public boolean equals(Object o) {
  32. if (this == o) return true;
  33. if (!(o instanceof Person)) return false;
  34. Person person = (Person) o;
  35. return name != null ? name.equals(person.name) : person.name == null;
  36. }
  37.  
  38. @Override
  39. public int hashCode() {
  40. return name != null ? name.hashCode() : 0;
  41. }
  42. }
  43.  
  44. public class Pet {
  45. private String nickname;
  46. private String vyd;
  47.  
  48. Pet(String nickname, String vyd) {
  49. this.nickname = nickname;
  50. this.vyd = vyd;
  51. }
  52.  
  53. @Override
  54. public boolean equals(Object o) {
  55. if (this == o) return true;
  56. if (!(o instanceof Pet)) return false;
  57. Pet pet = (Pet) o;
  58. if (nickname != null ? !nickname.equals(pet.nickname) : pet.nickname != null) return false;
  59. return vyd != null ? vyd.equals(pet.vyd) : pet.vyd == null;
  60. }
  61. }
  62.  
  63. ListMultimap<Person, Pet> map = ArrayListMultimap.create();
  64.  
  65. Person personFirst = new Person("name1");
  66. map.put(personFirst, new Pet("nick1", "vid1"));
  67. map.put(personFirst, new Pet("nick2", "vid2"));
  68. map.put(personFirst, new Pet("nick3", "vid3"));
  69.  
  70. Person personSecond = new Person("name2");
  71. map.put(personSecond, new Pet("nick1", "vid1"));
  72. map.put(personSecond, new Pet("nick2", "vid2"));
  73. map.put(personSecond, new Pet("nick3", "vid3"));
  74.  
  75. map.remove(personSecond, new Pet("nick3", "vid3"));
  76.  
  77. Map<Person, Collection<Pet>> mapResult = map.asMap();
  78.  
  79. public boolean put(@Nullable K key, @Nullable V value) {
  80. return this.get(key).add(value);
  81. }
  82.  
  83. public boolean remove(@Nullable Object key, @Nullable Object value) {
  84. Collection collection = (Collection)this.asMap().get(key);
  85. return collection != null && collection.remove(value);
  86. }
Add Comment
Please, Sign In to add comment