What is the best way of using Arrays.asList() to initialize a List

Sitansu :

I use this below code. Both are working fine in my application.

Case 1.

List<String> coreModules =
    new ArrayList<String>(Arrays.asList(
        "TOOLBAR_TO_DO_LIST",
        "TOOLBAR_PROPERTY",
        "TOOLBAR_PEOPLE",
        "TOOLBAR_INSURANCE",
        "TOOLBAR_BATCH",
        "TOOLBAR_INFORMATION_REFERENCE",
        "TOOLBAR_LR_PROPERTY",
        "TOOLBAR_CASE_FOLDER",
        "TOOLBAR_INSPECTION_RESULT",
        "TOOLBAR_MY_OFFICE"));

Case 2.

List<String> coreModules =
    Arrays.asList(
        "TOOLBAR_TO_DO_LIST",
        "TOOLBAR_PROPERTY",
        "TOOLBAR_PEOPLE",
        "TOOLBAR_INSURANCE",
        "TOOLBAR_BATCH",
        "TOOLBAR_INFORMATION_REFERENCE",
        "TOOLBAR_LR_PROPERTY",
        "TOOLBAR_CASE_FOLDER",
        "TOOLBAR_INSPECTION_RESULT",
        "TOOLBAR_MY_OFFICE");

But I have some questions:

  1. Which one is better one performance-wise?
  2. In which case prefer Case 2?
Lital Kolog :

Case 2 is better performance-wise BUT: it returns a List with an immutable size. Meaning you cannot add/remove elements to/from it:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

Arrays#asList

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the difference between List.of and Arrays.asList?

trouble initialize List<Object[]> Using Arrays.asList

What is the best practice to initialize an empty list in python ([] or list())?

What is the shortest way to initialize List of strings in java?

What is the best way to initialize a JavaScript Date to midnight?

What is the best way to copy a list?

Initialize List<> with Arrays.asList

What is the best way to initialize a bean?

What's the best way to initialize Quartz?

What is the best way to refresh a list using Firebase

What is the best way to get the list of column names using CsvHelper?

What is the best way to sum arrays using ECMASCRIPT 6 Generator/Functions

What is the best way to initialize the Variable in TensorFlow?

What is the best way to parameterize a pytest function using a fixture that returns a list?

What is the best way to merge nested arrays

What is the best way to Serialize/Deserialize nested Tuple2 arrays in dart (List<List<Tuple2>>)

What is the best way to center list items using bootstrap?

What is the best way to trim a list?

What's the best way to organize a display list using AS3?

What is the best way to set the followers list using parse.com

What is the best way to categorize/group and sort a list of elements using javascript?

What is the cleanest way to initialize dynamic list in Python?

What is the best way to merge 2 byte arrays?

What is the best way to print position of items in the list of strings using python

Create ArrayList of arrays using Arrays.asList

What's the best way to document an array of arrays of arrays using JSDoc?

What is the best way to combine two arrays using javascript?

What is the best way in Blazor to initialize a page that hits the DB to initialize itself

Java Generics: lower bound, can add an object to a list using Arrays.asList but not using List.add