2

I am very new to Java. I have recently come across fastutil and found ObjectArrayList class.

Is there any difference in performance if ObjectArrayList is used instead of ArrayList? What are the use cases for using ObjectArrayList?

abhijit
  • 345
  • 1
  • 3
  • 11

2 Answers2

4

According to fastutil documentation

A type-specific array-based list; provides some additional methods that use polymorphism to avoid (un)boxing.

There is a performance benefit to fastutil's implementation in cases where (un)boxing takes place.

The ObjectArrayList is backed by a generic type array. Whereas an ArrayList is backed by Object[] Really the performance between the two would be nominal. However FWIW, looks like this library provides primitive backed arrays IntArrayList DoubleArrayList where these boxing claims would actually see visible benefits in large datasets.

However, if you're new to Java. I'd highly recommend getting familiar with java.util.ArrayList before seeking out other variants.In most cases, taking the standard outweighs the performance benefit.

shinjw
  • 3,329
  • 3
  • 21
  • 42
  • It seems to be a bogus claim, though. Can you give an example of a situation where an ArrayList would use boxing but an ObjectArrayList would not? – Patrick Parker Aug 15 '18 at 06:25
  • Well... you can't create an ArrayList with a primitive. `ArrayList list`... which means that list.add(1) would have no choice but to be autoboxed. I haven't scrubbed through the source code and referencing their documentation. So the claim could in fact be bogus – shinjw Aug 15 '18 at 06:34
  • of course, but the same rule applies to ObjectArrayList. – Patrick Parker Aug 15 '18 at 06:38
  • Thanks for the replies guys. But I was really looking at anything special that fastutils might be doing since ArrayList is already there. – abhijit Aug 15 '18 at 09:26
  • Updated answer to answer the difference between the ObjectArrayList and ArrayList – shinjw Aug 16 '18 at 02:19
0

You should not be concerned with performance when you start learning a new language. Focus on the basics. Write code that runs, and then write code that is useful. Speed and efficiency only matters when the code you write affects the world around you (such as writing code for work). Do not worry about fastutil and ObjectArrayList. Finish your application with ArrayList and if your app is too slow for you only then should you find something faster.

Raymond Mutyaba
  • 950
  • 1
  • 9
  • 14
  • I have language constraints of using Java. And I have very limited time budget to process - well within 150 micros. I am using benchmarking tools (like JMH) to assess it anyway but it I wanted to know if anyone have any idea about what could be a reason as to why fastutil will go ahead with its own version of ArrayList – abhijit Aug 15 '18 at 09:29