1

I have an ArrayList with a bunch of MyObject objects. I want to keep an integer field in my object that is the key of this object in the ArrayList, so that I can easy get the position of this element in the ArrayList.

This gets a problem when I remove one object from this ArrayList, because the indexes get shifted to the left. What is the best way to avoid this problem?

Should I not remove the elements, but override them with null (so that the index gets not shifted) or should I rather iterate through the ArrayList after one removal to update all ID fields in the objects?

//EDIT: My purpose: Lets say I get some objects of that ArrayList and insert them in another list. Later when iterating through that second list, I want to get the key of the objects in the first ArrayList. So I must save the key at the object. Where are the benefits when using a Map in that case? The keys are only auto-increment integers.

2
  • 2
    Sounds like you'd be better off using a map. Check out: docs.oracle.com/javase/6/docs/api/java/util/Map.html Commented May 24, 2013 at 19:26
  • You could do this by just using a normal array, but I agree, map is a much better approach. Commented May 24, 2013 at 21:56

2 Answers 2

3

This it's possible using Map - HashMap

Example:

Map<Integer, MyObject> map = new HashMap<Integer, MyObject>();
MyObject obj = new MyObject();
Integer key = new Integer("1234");
obj.setKey(key);
map.put(obj.getKey(), obj);

for retrieve the object, call:

MyObject result = map.get(key);

result = null;

map.put(key, result);

Every key it's unique on the map so, if you change the value, it's dont affect the key.

In this case you can store a key with null value.

Otherwise, If you remove an index, and put it again, will solve:

        List<String> list = new ArrayList<String>();
        list.add("ONE");
        list.add("TWO");

        for (int index = 0; index < list.size(); index ++) {
            // remove the index that you will change to null
            list.remove(index);
            // put a null object in that index
            list.set(index, null);
            System.out.println(list.get(index));
        }

But I think that Map it's what you looking for. This second approach it's not thread safe and will cause crashes in case of concurrent threads

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I put another solution... is that what you are looking for?
You don't need to remove and add it afterwards. Just use list.set(index, null) to overwrite the old element. However a Map seems to be the best solution, thank you!
2

you better use a Map. Map is an object that maps keys to values. it cannot contain duplicate keys; each key can map to at most one value.

So in your case you can store the id and the object as key,value pair in the map. And from there you can directly access the object of related id.

3 Comments

What is the exact difference between a Map and an ArrayList? I just want to use an auto-increment integer as a key, so this works with both of them. I can get the value by providing a key with both of them. So what is the advantage in my case?
you can access map in a easier way by a id than arraylist. map is created to store (key, value) pair data structure. and according to your question that is what you need.
If iteration order is important, you can use a LinkedHashMap or a TreeMap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.