blah_crusader
blah_crusader

Reputation: 434

Java: Multiple keys, one value HashMap, pointing to the exact same value

I'm looking for a way to map multiple keys to the same value without using extra memory by adding this value twice. Normally you would just do:

 Map<Integer, Integer> map = new HashMap<>();
 map.put(065,600);
 map.put(070,600);

But it is my understanding that the value 600 is now stored in memory twice. Is there a way to avoid this such that they point to the exact same value? Thanks a bunch ! Peace out

Upvotes: 2

Views: 1645

Answers (1)

Andres
Andres

Reputation: 10727

Try this:

 Map<Integer, Integer> map = new HashMap<>();
 Integer i = 600;
 map.put(065,i);
 map.put(070,i);

Upvotes: 2

Related Questions