Java: the best way to keep an arbitrary ArrayList index

I know that I cannot save the value in an ArrayList index that has not been used yet, i.e. smaller size. In other words, if myArrayList.size () is 5, then if I try to do

myArrayList.set(10, "Hello World") 

I will get an error outside the bounds. But my application needs this. Is there a more elegant way besides the cycle of storing zeros in each of the intermediate slots?

It seems to me:

  • This behavior in Vector
  • If I need to be able to randomly access (i.e. an element in pos X), then my options are Vector and ArrayList.
  • I could use a HashMap and use the index as a key, but it is really inefficient.

So, what is an elegant solution to what looks like a common case. Something is missing me ...

+4
source share
7 answers

I could use a HashMap and use the index as a key, but it is really inefficient.

It depends. If the indexes you use are very sparse, it is best to use a map. If indexes are usually closely related to each other, I think there is no better way than filling it with zeros. Just write a utility function for it that you can use again and again, instead of repeating the loop wherever needed, something like this:

 private void padTo(List<?> list, int size) { for (int i=list.size(); i<size; i++) list.add(null); } 
+4
source

Instead, you can use Map<Integer, MyClass> . In particular, if you use a HashMap , it will also be O(1) - although it will be slower than an ArrayList .

+3
source

You can use TreeMap<key, value> , which is sorted in natural order with value .

Here you can save the value as an index. You can insert any value, it should not be in order. This seems like the easiest solution.

+3
source

It looks like you need a regular array:

  • You need random access
  • You want to specify some big size
+1
source

If you definitely need to use a list rather than a map, it is best to override the methods for adding and setting arrays to put zero in indexes first. There is no other better way IMO

+1
source

HashMap is probably much less effective than you think, try it. Otherwise, I cannot think of it more elegantly than looping and filling with zero. If you want at least the elegance of presentation, then you can always subclass ArrayList and add the expandSet (position, value) method to hide all loops and the like. Perhaps this is not an option? If you have not just a utility method somewhere else, but it is not so nice imho, although it will work with other types of lists too, I think ...

Maybe the wrapper class will be the best of both worlds, or maybe it just entails unnecessary overhead ...

+1
source

If you are looking for a sparse array (where most of the indices will be empty), then it is best to use a map of some kind (possibly a HashMap). Any array-esk solution will be forced to reserve space for all empty indexes, which is not very efficient in terms of space, and HashMap is fast enough for most common purposes.

If you end up filling the array with some n, you will need to add zeros in the loop to get the index you need. You can make this somewhat more efficient by providing it with the initial capacity of the number of elements that you ultimately want to save (this prevents the ArrayList from resizing). new ArrayList(n) will work fine. Unfortunately, there is no easy way to make it a specific size to start with, other than adding things to the loop when you do it.

0
source

Source: https://habr.com/ru/post/1395804/


All Articles