Groovy- Difference between List, ArrayList and Object Array

39,290

Solution 1

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.

Solution 2

You can find differences between ArrayList and LinkedList, these are implementations of List(interface). Each implementation has different methods. You can see these methods in:

*Methods LinkedList

*Methods ArrayList

List can't be compared with ArrayList.

Solution 3

List is an interface and ArrayList an implementation with certain characteristics. Like all other programming languages, also Java has certain containers for certains problems. You can get the initial grasp here: http://docs.oracle.com/javase/1.5.0/docs/guide/collections/overview.html

Share:
39,290

Related videos on Youtube

user1207289
Author by

user1207289

Updated on July 09, 2022

Comments

  • user1207289
    user1207289 almost 2 years

    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!

  • user1207289
    user1207289 about 9 years
    Thank you for detailed explanation . I'll go through it and try to understand.
  • user1207289
    user1207289 about 9 years
    Thanks for the links. I'll go through them.