How to create an ArrayList?

Below are two ways to create an ArrayList :

 List<String/*or other object*/> arrList = new ArrayList();//Imports List, ArrayList ArrayList<String/*or other object*/> arrList = new ArrayList();//Imports just ArrayList 

What is the difference? Which method should be used?

+6
source share
9 answers

The first form is recommended for the public interface of a class (for example, declaring attributes in a class). This is a well-known tip that you should program for the interface, and not for a specific implementation (as indicated in Design Patterns ) - the number of classes that you need to import have little to do with the quality of the code and good design.

The advantage of the first form is that it will allow you to easily replace implementations later if the need arises, while the second implementation inherits the implementation class, making your code more difficult to develop and less flexible.

There are times when you can declare and use specific classes, though (for example, for low performance in performance). But the public interface of the class should use interfaces if possible, this will facilitate the implementation of future changes.

+6
source

The first methods are called coding for the interface

The second is called coding for implementation.

The advantage of the second is that it gives you the flexibility to change the implementation class without changing the code. For example, today you use ArrayList , but if you want to change it to LinkedList tomorrow, just change the class of the impediment in the list definition.

+2
source

By doing this, as the first example, you can assign a List to any data structure that implements the List interface.

This adds flexibility to your program because you can easily change the implementation.

+1
source

The first allows you to freely modify a specific class (ArrayList) if you later realize that another List implementation is better suited to your task. You will only need to change one line, since other places using arrList are expecting something that implements List, not ArrayList .

In the second version, if you decide to make this change, you will have to change every place waiting for an ArrayList.

As mentioned above, this is called interface coding.

+1
source

A list is an interface. ArrayList is a class that implements the List interface.

0
source

The difference is that it hides a specific implementation from calling methods that return a List. This is usually a good practice. If you return List , then you will have the freedom to change the underlying implementation later without worrying about the effect of ripple on changing a bunch of other code. If you don't need elements like List, such as List.get(index) , you can go even further and simply specify Collection<String> col = new ArrayList<String();

0
source

If you want to create an ArrayList , then only the second will be correct. The first creates a List , which is implemented by the ArrayList class, but in many cases they are not interchangeable. This question has a pretty good answer.

0
source

Contrary to popular belief, I say that we should use the second form

 ArrayList<X> list = new ArrayList<>(); 

This line is an implementation detail; we should be as specific as possible in our implementation details.

This is not part of the public API that should be abstracted; the arguments for "code versus interface" are not applicable here.

0
source

I see that many people recommend using the interface, I would say the following:

  • If you think that the person using your code really needs to know the implementation details that you are using, use a specific class. For example, if you are writing a high-performance library that has methods that return a specific implementation, such as LinkedList, which requires special care when processing).

  • In most other cases where you can avoid over-granularity, use interfaces.

0
source

Source: https://habr.com/ru/post/952665/


All Articles