#266 HashMap Behavior with Key Collisions

Companies:
Aston
Themes:
JavaCollections & GenericsCode Review

What will be printed?

import java.util.*;

class Main {
    private int i;

    public Main(int i) {
        this.i = i;
    }

    public static void main(String[] args) {
        Map<Main, Object> map = new HashMap<>();
        map.put(new Main(1), null);
        map.put(new Main(2), null);
        map.put(new Main(1), null);
        System.out.println(map.size());
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return -1;
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in