Best practice for declaring ArrayList classes or collections

Can anyone explain what the difference is in the following ArrayList declaration for storing String .

 List type1 = new ArrayList(); List type2 = new ArrayList<String>(); List<String> type3 = new ArrayList<String>(); ArrayList<String> type4 = new ArrayList<String>(); List<String> type5 = null; ArrayList<String> type6 = null; 

so from the above declaration it is best to declare an ArrayList for String and why?

+6
source share
6 answers

The first two use raw types. This means that your list is not safe for all types. The compiler will allow you to store integrals inside, even if your intention is to have a list of strings. The compiler will generate a warning that you should not ignore.

The third is correct. It tells the compiler that you intend to use a list of strings and that the specific implementation you have selected is an ArrayList. If you change your mind later and want to use LinkedList, this line of code will be the only one you need to change.

The fourth tells the compiler that your program does not need only a list. He needs this list as an ArrayList. This is normal if your code really needs to call methods specific to ArrayList and they are not in the List interface. But in 99.9% of cases this is not so, and you should prefer the third.

The last two declare the variable and initialize it to zero, rather than creating a list. This is a designer smell. Before using the list, you need to make sure that it is not zero. It is much safer to initialize it with the correct list.

+4
source
  • List type1 = new ArrayList();
  • List type2 = new ArrayList<String>();

    You have a raw type ArrayList and List , you should not use them (although this is legal code):

    ... The term "unchecked" means that the compiler does not have enough type information to perform all type checks necessary to ensure type safety

  • List<String> type3 = new ArrayList<String>();

    It is safe, and with Java 7 it can use the diamond operator and is rewritten as:

    List<String> type3 = new ArrayList<>();

    This is best since it programs the interface, which means that you can change type3 to any other class that implements List .

  • ArrayList<String> type4 = new ArrayList<String>();

    This object is specific, you can never change the type type4 to something else, and not to an ArrayList .

The last two cannot be compared with the rest, you just give them a default value, sometimes you are forced, sometimes you can skip this initialization.

+5
source

type1 and type2 Raw types and should be avoided.

type3 and type4 are good (but not IMO the best, and I will come back to that) - I would prefer type3 to type4 because it uses an interface type (and you have to program the interface). For example, you can use Arrays.asList("a","b") with type3 .

type5 and type6 are similar to type3 and type4 , but I would recommend that you not initialize to null .

Finally, with Java 7+, I would recommend the diamond operator. For instance,

 List<String> type7 = new ArrayList<>(); 
+2
source

In general, it is good practice to keep the left side of the least specific type needed to perform your functions. However, you should always parameterize parameterized types and avoid raw types. Therefore you should use:

 List<String> type3 = new ArrayList<String>(); 

Note that with Java 7 you can also omit the type on the right side:

 List<String> type3 = new ArrayList<>(); 

Later, if you want to use LinkedList instead, you can simply update the line of code in which you create the list, without having to change all the links to it.

+2
source
  • 1 and 2 are raw type declarations. You can add any type of object to this list that you like, but you cannot guarantee type safety at compile time.

    Declaring a list this way is usually not approved , as it can be a very simple mistake to accidentally put an object on a list that you should not have. The real caveat here is that this error will not occur until run time , while using a type parameter will have this the error in the expression itself is compilation time , which is much easier to deal with.

  • 3 is the preferred approach for declaring a list (or collection of any kind), since you are associated with an interface instead of a specific implementation, It also includes type restrictions that guarantee compilation type security.

  • 4 - declaration of a specific type of ArrayList . If you need a specific type, then everything is fine, but you have only a few cases at your disposal.

  • 5 and 6 - repetitions 3 and 4, respectively; the difference is that they are both initialized to zero.

+2
source

Or List<String> type3 = new ArrayList<String>(); or List<String> type5 = null; .

It is always better not to use raw types such as List and ArrayList , and more flexible to declare interface type variables instead of implementation types.

Regarding whether you want to initialize the variable to null and create an instance of List later (or assign a variable to the result of the method that returns an instance of List ), or create an instance when you declare it, both options are equally true, in my opinion.

+1
source

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


All Articles