Guest User

Untitled

a guest
Jun 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class App {
  5.  
  6. public static void main(String[] args) {
  7. Object object = new Object();
  8. Map<Book, Object> map = new HashMap<>();
  9.  
  10. for(int i = 0;i<100 ;i++){
  11. Book book =new Book(i);
  12. map.put(book, object);
  13. }
  14.  
  15. System.out.println(map.size());
  16. map.remove(new Book(50));
  17. System.out.println(map.size());
  18.  
  19. }
  20. }
  21. class Book {
  22. int i;
  23.  
  24. public Book(int i) {
  25. this.i = i;
  26. }
  27.  
  28. @Override
  29. public int hashCode() {
  30. return i; //or just 1
  31. }
  32. @Override
  33. public boolean equals(Object o) {
  34. if (this == o) return true;
  35. if (o == null || getClass() != o.getClass()) return false;
  36.  
  37. Book book = (Book) o;
  38.  
  39. return i == book.i;
  40. }
  41.  
  42. }
Add Comment
Please, Sign In to add comment