Skip to main content

How can you improve the HashMap look up code

Is there an issue with the code below.  If so how do you resolve it

package com.javahowdoi.challengeq;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

public class MapQ {
    @Data
    @AllArgsConstructor
    public static class MapInner {
        int i;
        int j;
    }
    public static void main(String[] args) {
        Map<MapInner, Integer> m = new HashMap<>();
        MapInner mi = new MapInner(0,1);
        m.put(mi, 0);
        mi.setI(2);
        System.out.println(m.get(mi));
    }
}

Here's the answer 

Comments