Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList with returned index on add value

I am looking for a java data structure similar to an ArrayList that when I do an add or a push with only a value argument an index will be returned for me automatically.

For example:

ArrayList<String> elements = new ArrayList<String>();

String element = "foo";
String elementTwo = "bar";

int index1 = elements.add(element); //note this does not exist, i.e. returns bool in api
int index2 = elements.add(elementTwo);

System.out.println(elements.get(index1)); //would give "foo"

I could see writing a wrapper class around ArrayList that manages a counter that is incremented on every add operation and invoking:

ArrayList.add(int index, E element)

Do you really need to write a wrapper around ArrayList for this? This seems like something simple enough to be provided out of the box somewhere?

Edit:

I need the index (key) to be fixed and unique for this usecase. A map was suggested and I agree with that. Does anyone know of a map implementation that gives you an automatically (uniquely) generated key on a value insert? I am just trying to decide if I need to implement my own wrapper for this.

like image 518
Ryan R. Avatar asked Apr 25 '12 03:04

Ryan R.


People also ask

How do I add an index value to an ArrayList?

The java. util. ArrayList. add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).

How do I return an index from an ArrayList?

The indexOf() method is used to get the index of the first occurrence of an element in an ArrayList object. The element whose index is to be returned. Return Value: The index of the first occurrence an element in ArrayList, or -1 if the element does not exist.

Why does ArrayList add return true?

It's because of the Collection interface, which List extends. A list's add method always returns true .

What does ArrayList add return?

Return: It always return "true". Don't worry about the Boolean return value. It always there as other classes in the collections family need a return value in the signature when adding an element.


2 Answers

The element will be added at the end of the list. So you can use elements.size()-1 to get the new elements index.

Note that this will not work reliable if multiple threads are modifying the list at the same time.

EDIT: Also note that it might not be a good idea to use an ArrayLists index as a unique ID because an elements index can change (for example when you remove an element or insert a new one using add(int, Object)). If this is a problem depends on what you want to do with the index: If you only need it for a short time after adding an element and can be sure that the list is not modified in the meantime, there is no problem. In the other case even a method returning the index when calling add(Object) would not help because the index does not get updated in anyway. To prevent this issue you can:

  • Make sure you never remove elements from the list and never add elements using add(int, Object).
  • Instead of removing elements you could also set them to null using the method set(int, null). This way no elements index will change.
  • Use some other data structure like for example a map with a custom ID like helloannalil suggests in his answer.

EDIT 2: I did not find a appropriate, ready to use implementation (but this does not mean there is none, of course). To suggest a good solution, more information on the intended use of the data structure is needed, but here are some ideas and notes:

  • If the maximum number of elements is not to large, an ArrayList could be used and the elements index represents the ID. As stated above, to remove an element it can be set to null so that no indices are changed. When inserting, positions with null values can be reused.
  • You can also use one of the two methods show in this answer: https://stackoverflow.com/a/8939049/1347968 (keywords AtomicLong or IdentityHashMap)
  • Do not depend on the "uniqueness" of Object.hashCode() or System.identityHashCode(Object) as it is not guaranteed (try it by running the example at the bottom of Suns/Oracles Bug #6321873).
like image 137
siegi Avatar answered Oct 02 '22 12:10

siegi


Well what I do in that cases (I love ArrayLists) is to get the last index by asking the size of the list:

String thing = "theThing";
List<String> strList = new ArrayList<String>();
strList.add(thing);
int indexOfThing = strList.size() - 1;

I mean, is easier than implement your own List and just works.

like image 27
Omar Avatar answered Oct 02 '22 11:10

Omar