Why not always use ArrayLists in Java and not simple OL arrays?

The quick question here is: why not ALWAYS use ArrayLists in Java? They obviously have equal access speed in the form of arrays, in addition to additional useful functions. I understand the limitation is that it cannot contain primitives, but this is easily mitigated with wrappers.

+4
java
source share
5 answers

If you need a set of primitives, then an array can be the best tool to work with. Boxing is a relatively expensive operation. For a collection (not including maps) of primitives that will be used as primitives, I almost always use an array to avoid re-boxing and unpacking.

I rarely worry about the performance difference between an array and an ArrayList . If a List provides better, cleaner, more convenient code, then I will always use List (or Collection or Set , etc., if necessary, but your question was about an ArrayList ), unless that’s some good reason not to of this. Performance is rare , which is a compelling reason.

Using Collection almost always leads to better code, partly because arrays don't go very well with generics, as Johannes Weiss noted in a comment, but also for many other reasons:

  • Collections have a very rich API and a large number of implementations that can (in most cases) be trivially replaced by each other
  • A collection can be trivially converted to an array if it is useful to use an array version
  • Many collections grow more gracefully than the array grows, which can be a performance issue.
  • Collections work very well with generics, arrays are pretty bad
  • As TofuBeer pointed out, covariance of arrays is strange and can act in unaffected ways that no object will act in. Collections handle covariance in expected ways. Arrays
  • must be sorted manually by their task, and if the array is not full, you need to track this yourself. If you need to resize the array, you must do it yourself.

All this together, I rarely use arrays and only use ArrayList little more often. However, I often use List (or just Collection or Set ). My most frequent use of arrays is when a stored element is primitive and will be inserted and accessible and used as a primitive. If boxing and unpacking seem so fast that it becomes a trivial consideration, I can reconsider this decision, but it’s more convenient to work with something, store it in the form in which it always refers. (That is, 'int' instead of 'Integer'.)

+5
source share

Many projects simply use ArrayList or HashMap or whatever to cope with all the needs of their collection. However, let me put one warning on this. Whenever you create classes and use them in all your code, if possible, refer to the interfaces that they implement, and not to the specific classes that you use to implement them.

For example, instead:

 ArrayList insuranceClaims = new ArrayList(); 

do the following:

 List insuranceClaims = new ArrayList(); 

or even:

 Collection insuranceClaims = new ArrayList(); 

If the rest of your code knows it only through the interface that it implements ( List or Collection ), then replacing it for another implementation becomes much easier in the future if you find that you need another. I saw how this happened just a month ago, when I needed to change the regular HashMap for an implementation that would return me the elements in the same order I entered them when it was time to iterate over all of them. Fortunately, such a thing was available in the Commons Jakarta Commons collections, and I just changed A for B with changing only one line, because both are implemented by Map.

+10
source share

This is a case of premature non-optimization :-). You should never do something because you think it will be better / faster / make you happier.

ArrayList has extra overhead, if you do not need additional ArrayList functions, then it is useless to use ArrayList.

Also for some things you can do with a list, there is an Arrays class, which means that ArrayList provided more options than arrays, less rights. Using these functions may now be slower than using an ArrayList, but you would need to profile it to be sure.

You should never try to do something faster without being sure that it starts slowly ... which implies that you have to go ahead and use ArrayList until you find out that this is a problem and slow down the program. However, there should also be common sense - ArrayList has overhead, the overhead will be small but cumulative. In the profiler it will be difficult to notice that all this is a little overhead and a little overhead. So common sense would say if you don't need ArrayList functions, you shouldn't use it unless you want to die in thousands of cuts (performance wise).

For the internal code, if you find that you need to switch from arrays to ArrayList, the probability in most cases is quite simple ([i] becomes get (i), which will be 99% of the change).

If you use for-each look (for (value: items) {}), then you also do not need to change the code for this.

Also, going with what you said:

1) equal access speed, depending on your environment. For example, there are no built-in methods in the Android VM (this is just a direct interpreter, as far as I know), so access to this will be much slower. There are other operations in ArrayList that can cause a slowdown, depending on what you are doing, regardless of the virtual machine (which can be faster with the stright array, again you will need to profile or check the source to be sure).

2) Wrappers increase the amount of memory used.

You should not worry about speed / memory before commenting on something, on the other hand, you should not choose what you know as a slower option, if you have no good reason.

+4
source share

Performance should not be your main concern.

Use the List interface, where possible, select a specific implementation based on actual requirements ( ArrayList for random access, LinkedList for structural changes, ...).

You should be concerned about performance.

Use arrays, System.arraycopy , java.util.Arrays and other low-level materials to squeeze every last performance.

+4
source share

Well, it’s not always blind to use something that is not suitable for work. Always start using lists, select ArrayList as your implementation. This is a more OO approach. If you don’t know that you definitely need an array, you will find that not binding yourself to a specific List implementation will be much better for you in the long run. Run it first, optimize later.

+2
source share

All Articles