Mutating key values

Report a typo

Now you must have a good understanding of how IdentityHashMap works! What will the following code print?

public class IdentityHashMapDemo {
    public static void main(String[] args) {
        Person james = new Person("James Gosling", 1955);
        Person guido = new Person("Guido van Rossum", 1956);

        Map<Person, String> map = new IdentityHashMap<>();
        map.put(james, "Java");
        map.put(null, "Python");

        james.setName(guido.getName());
        james.setBirthYear(guido.getBirthYear());
        guido = null;

        System.out.printf("%s, %s", map.get(james), map.get(guido));
    }
}
Enter a short text
___

Create a free account to access the full topic