Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- public class App {
- public static void main(String[] args) {
- Object object = new Object();
- Map<Book, Object> map = new HashMap<>();
- for(int i = 0;i<100 ;i++){
- Book book =new Book(i);
- map.put(book, object);
- }
- System.out.println(map.size());
- map.remove(new Book(50));
- System.out.println(map.size());
- }
- }
- class Book {
- int i;
- public Book(int i) {
- this.i = i;
- }
- @Override
- public int hashCode() {
- return i; //or just 1
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Book book = (Book) o;
- return i == book.i;
- }
- }
Add Comment
Please, Sign In to add comment