How should I get an object reference which was inserted as a key in ArrayList?
-
I have a hashmap in which the keys can change, But the object reference used as key does not change. For example, public class MyObject { int index; String str; } public class HashMapTest { public static void main(String[] args) { HashMap<MyObject, String> map = new HashMap<MyObject, String>(); MyObject mo = new MyObject(); MyObject mo1 = new MyObject(); MyObject moPtr; mo.index = 1; moPtr = mo; map.put(moPtr, "abc"); mo1.index =100; mo.index = 2; //moPtr = null; map.put(moPtr, "pqr"); System.out.println("value: " + map.get(moPtr)); Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); System.out.println(((MyObject)pairs.getKey()).index + " = " + pairs.getValue()); it.remove(); // avoids a ConcurrentModificationException } } } Now, problem is I want to find some object with given index = 2, I should get object reference for mo. I am not able to understand how should I do that. Could someone please help me with this.
-
Answer:
With the code you've given, the only way to do it is to iterate over the contents of the map. (And taking note of the fact that, as says, the code you've given will throw a NullPointerException, but that's easily fixed. Your whole bit with moPtr is just plain wrong and you should get rid of it.) The whole point of a Map is to be able to retrieve an object with a key of the same type. If what you want is to index on the index, you'd need a map more like this: Map<Integer, MyObject> map = ...; map.put(mo.index, mo); To be able to use the data structure you have, you'd need another MyObject object, like this: MyObject key = new MyObject(); key.index = 2; map.get(key); Now, the problem with this is that key and mo1 are different objects; they won't hash to the same value and they won't be equals (using the default methods), which is what HashMap relies on. You need to go in to MyObject and override hashCode and equals so that they rely solely on the value of index. I'm leaving that as an exercise for the reader, but it's not difficult.
Joshua Engel at Quora Visit the source
Other answers
Is this Java? Your code iz wrong MyObject moPtr; moPtr.index = 1; will give NullPointerException Why are you calling it a Ptr? Are you a C++ developer? It seems to me like you don;t understand how references in Java work. Might want to read http://www.javaranch.com/campfire/StoryCups.jsp
Jayesh Lalwani
Related Q & A:
- How do I get a value from a JObject?Best solution by Stack Overflow
- How can I get an audio alert when I get a new e-mail?Best solution by Yahoo! Answers
- How do I get my computer to notify me when I get a email?Best solution by Yahoo! Answers
- How can I get a text alert when I get an email?Best solution by Yahoo! Answers
- How do I get my parents to let me go to a boarding school?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.