List or ArrayList in Java?

1

What is the difference between the following implementations?

ArrayList<String> xxx = new ArrayList<>();
List<String> zzz      = new ArrayList<>();
    
asked by Orici 07.06.2018 в 22:55
source

3 answers

3

List is a collection, and a collection can be interfaces and abstract class that allows us to identify the objects independently of the implementation. That is, they are generic.

While, a ArrayList is a container that contains an implementation of the List collection.

Here you can see an example of the collections and their relationships in Java: link

Based on this, List is a generic interface that represents an ordered collection of elements that can be repeated.

While, two general-purpose lists would be the classes LinkedList and ArrayList and for a specific purpose, < Vector and CopyOnWriteArrayList .

In your doubt, the ArrayList is an array that is handled as a class and has no fixed size. It is efficient when many access is made and in this case the elements are added at the end. It allows intermediate elements to be eliminated, but this will cause a displacement of indexes.

Instead, for a class LinkedList we will have a linked list in which the elements are added anywhere in the list very easily. Here, to find an item you have to go through the list. It is efficient when the size fluctuates and especially in central positions.

What happens if you use List as you do in your case ?, that you could change the type of zzz to < em> LinkedList in its definition and everything would be 100% compatible and easily. However, in the first case of xxx you would have to change the type in all the sites because you are implementing a ArrayList and you could have problems if you change it to LinkedLIst . That's why we recommend using List , as you do with zzz .

    
answered by 07.06.2018 / 23:10
source
0
public static void insert(List<Dato> datos) {
  for(Dato dato:datos){
    db.executeUpdate("insert into datos(desc,cantidad) values(:desc,:cantidad)",
    new Object[]{dato.getDescripcion(),dato.getCantidad()});
  }
}


public static void insert(Dato[] datos) {
  for(Dato dato:datos){
    db.executeUpdate("insert into datos(desc,cantidad) values(:desc,:cantidad)",
    new Object[]{dato.getDescripcion(),dato.getCantidad()});
  }
}

As you can see, it is practically the same, the difference is that when invoking the first method you can pass as an ArrayList, LinkedList, Vector or your own class that implements the List interface

a link that can help you link

    
answered by 07.06.2018 в 23:08
0

List is an interface, while ArrayList is one of the many implementations of the List interface (in turn List extends the Collection interface >).

A priori, then, there is no difference, polymorphically you will be working with an ArrayList. But it seems more correct to use the second sentence and work against the interface and never against the specific type. In this way you have the flexibility to instantiate another implementation different from the List interface (LinkedList for example) and your code will continue to work the same, thanks to the contracts are maintained.

    
answered by 07.06.2018 в 23:13