Andrew Johnson Andrew Johnson - 5 months ago 29
Swift Question

What is the equivalent Java collection to a Swift array?

I've noticed Java has a lot of collection types and I am unsure which one to use. I come from a Swift programming background. What is the closest or equivalent Java collection to a Swift array? From my research, ArrayList, seems to be the closest. I just want to confirm this.

Answer

The List interface in Java does expose functionalities similar to the Swift Array struct.

There are several Java implementation of List:

  1. AbstractList
  2. AbstractSequentialList
  3. ArrayList
  4. AttributeList
  5. CopyOnWriteArrayList
  6. LinkedList
  7. RoleList
  8. RoleUnresolvedList
  9. Stack
  10. Vector

Each one is particularly optimised for some kind of task so you should pick the one that better fits your need.

Finally yes, ArrayList is probably the most commonly used.

Important: Class vs Struct

Please keep in mind that the previous list is about Java classes while the Swift Array is a struct. There are important differences (reference type vs value type) about how these 2 types behave.