Java map based on object reference?
I think IdentityHashMap will do the trick. However, both strings will point to the very same instance since you used a string literal. Try s1 = new String(«hi!») and s2 = new String(«hi!») together with an IdentityHashMap instead.
Solution 2
Check out Guava’s Multimaps (implementations listed on the Multimap interface page).
Solution 3
You should have a look at IdentityHashMap.
This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).
Related videos on Youtube
rybosome
Comments
So, I’d like to use a java map where the keys are an object. but rather than keying on the object’s value, they key on the object ID. So, something like the following would be totally valid code:
Map map = new HashMap(); String s1 = "hi!"; String s2 = "hi!"; map.put(s1, 10); map.put(s2, 47);
Is this possible? Is there a simple way to do this without building an object ID or something overly cumbersome in my class? Basically, I need a way to associate an ever-changing list of values with a given object. This list of values will potentially be different for objects that have the same value, hence why the default map doesn’t work. Other than refactoring my class to do this myself (not really an option, given the time) is there anything that I could use? Thanks. EDIT: Further information. The example above was just an example. What I will be using this for is the implementation of a Uniform-Cost search algorithm. For any given node in a search with this algorithm, one must also have the path that has been taken so far. The reason a value-based hash map doesn’t work is that this algorithm can reiterate over already-explored nodes. The paths would be different at this point, although the value of «where am I now?» is identical.
If you don’t implement equals() and hashCode() by yourself, you’re basically inheriting Object s implementation, which compare by identity, not equality.
Didn’t think about it. +1 But maybe the asker is searching for a multi-map and he doesn’t formalize it the correct way. I find that design (s1 and s2 substantially equal but pointing to different values) not good.
This is probably a sensible approach. I don’t see the point of keeping a reference to two distinct but equal strings and then lookup the values using both of ones. I bet that it’s just poor design, and has to be corrected using a MultiMap
I should have been more clear in my example, but the object in question is not a String. While it is possible that the design could be better, I am DEFINITELY wanting to associate differing values with specific objects which can be semantically identical.
So if the Object is not a String, and does not override equals() and hashCode(), then equals() and hashCode() work by checking identity. So you don’t need anything else than a standard Map. Do you override equals() and hashCode() ? If the answer is ‘yes’ you’re 100% right.
Difference Between Hashmap and Map in Java
- the Map Interface in Java
- the HashMap Class in Java
- Use a Map Reference to Hold Objects in Java
- Use a Map Reference to Hold Objects in Java
This tutorial introduces the main differences between Map and HashMap in Java.
In Java, Map is an interface used to store data in key-value pair, whereas HashMap is the implementation class of the Map interface. Java has several classes ( TreeHashMap , LinkedHashMap ) that implement the Map interface to store data into key-value pair. Let’s see some examples.
the Map Interface in Java
The Map interface alone can not be used to hold data, but we can create an object of its implementation classes and then use the Map reference to hold the object. Here, we use the HashMap class to store data and the Map interface to hold the reference of this class. See the example below.
import java.util.HashMap; import java.util.Map; public class SimpleTesting public static void main(String[] args) MapString, Integer> map = new HashMap<>(); map.put("One", 1); map.put("Two", 2); map.put("Three", 3); System.out.println(map); > >
the HashMap Class in Java
HashMap is an implementation class of the Map interface. So, we can use it to create a collection of key-value pairs. See the example below.
import java.util.HashMap; public class SimpleTesting public static void main(String[] args) HashMapString, Integer> map = new HashMap<>(); map.put("One", 1); map.put("Two", 2); map.put("Three", 3); System.out.println(map); > >
Use a Map Reference to Hold Objects in Java
Since Map is an interface, we can use it to hold the reference of its implementation classes such as HashMap , TreeMap , etc. We can hold a TreeMap or HashMap object into the Map interface. See the example below.
import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class SimpleTesting public static void main(String[] args) MapString, Integer> map = new HashMap<>(); map.put("One", 1); map.put("Two", 2); map.put("Three", 3); System.out.println(map); MapString, Integer> tmap = new TreeMap<>(map); System.out.println(tmap); > >
Use a Map Reference to Hold Objects in Java
It is an important example of using the Map reference while working with its implementation classes. See, we have a method that takes a Map object as an argument. So, at the time of call, we can pass the object of any classes such as HashMap or HashTable . See the example below.
import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class SimpleTesting static void printMap(MapString, Integer> map) for(String key : map.keySet()) System.out.println(key+":"+map.get(key)); > > public static void main(String[] args) HashMapString, Integer> hashmap = new HashMap<>(); hashmap.put("One", 1); hashmap.put("Two", 2); hashmap.put("Three", 3); printMap(hashmap); TreeMapString, Integer> tmap = new TreeMap<>(hashmap); printMap(tmap); LinkedHashMapString, Integer> lmap = new LinkedHashMap<>(hashmap); printMap(lmap); > >