I was looking to understand difference between groovy List
, ArrayList
and Object Array
but couldn't find real (simple) examples. Like, what can we do with Array
, that can't be done with List
or ArrayList
? I understand that Array is a fixed sequence of objects. Just to mention that I've looked at this , this and this in java and trying to understand the points mentioned there.
I hope I am describing my issue clearly, but let me know if I am not clear or totally missing the point. Can someone point me to the right direction? Thank You!
Array is a fixed length data structure whereas ArrayList is a variable length Collection class. We cannot change length of array once created in Java but ArrayList can be changed. We cannot store primitives in ArrayList, it can only store objects. But array can contain both primitives and objects in Java.
The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.
By default, Groovy creates an instance of java. util. ArrayList. However, we can also specify the type of list to create: def linkedList = [1,2,3] as LinkedList ArrayList arrList = [1,2,3] Next, lists can be used to create other lists by using a constructor argument: def copyList = new ArrayList(arrList)
Whereas ArrayList can hold item of different types. An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.
Yes, an Array
is a data structure with a fixed size. It is declared as having a type that describes what elements it can hold, that type is covariant (see here for covariant vs contravariant). The Array
knows its type at runtime and trying to put anything inappropriate in the Array
will result in an exception.
In Groovy, Arrays are not really idiomatic due to being low-level and inflexible (fixed-size). They are supported for interoperation with Java. Typically people using Groovy prefer List
over Array
. Groovy does try to smooth out the differences, for instance you can use the size
method on an Array
to get the number of elements (even though in Java you would have to
use the length
property).
(In Ruby the data structure most closely resembling a list is called Array
, so people coming to Groovy or Grails from Rails without a Java background tend to carry over the nomenclature with resulting confusion.)
java.util.List
is an interface that describes basic list operations that are implemented by the different kinds of Lists. Lists use generic type parameters to describe what they can hold (with types being optional in Groovy). The generic types on Lists are invariant, not covariant. Generic collections rely on compile-time checking to enforce type safety.
In Groovy when you create a list using the literal syntax (def mylist = []
) the java.util.ArrayList
is the implementation you get:
groovy:000> list = ['a', 'b', 'c']
===> []
groovy:000> list instanceof List
===> true
groovy:000> list.class
===> class java.util.ArrayList
groovy:000> list.class.array
===> false
groovy:000> list << 'd'
===> [d]
groovy:000> list[0]
===> a
In order to create an array you have to add as (type)[]
to the declaration:
groovy:000> stringarray = ['a', 'b', 'c'] as String[]
===> [a, b, c]
groovy:000> stringarray.class
===> class [Ljava.lang.String;
groovy:000> stringarray.class.array
===> true
groovy:000> stringarray << 'd'
ERROR groovy.lang.MissingMethodException:
No signature of method: [Ljava.lang.String;.leftShift() is applicable
for argument types: (java.lang.String) values: [d]
groovy:000> stringarray[0]
===> a
There are already several questions, ArrayList Vs LinkedList and When to use LinkedList<> over ArrayList<>?, which cover the differences between LinkedList
and ArrayList
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With