24

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?
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Sitansu
  • 3,225
  • 8
  • 34
  • 61

2 Answers2

42

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

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Lital Kolog
  • 1,301
  • 14
  • 39
  • 3
    Javadoc says (for Arrays.asList) : `Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)`. So it is not immutable. – Arnaud Denoyelle Dec 12 '13 at 09:00
  • 3
    @Arnaud Denoyelle, try adding or removing an element from the list and see for yourself :) you can change an existing element but not adding new ones. – Lital Kolog Dec 12 '13 at 09:00
  • `java.lang.UnsupportedOperationException` You are right. +1 for you. – Arnaud Denoyelle Dec 12 '13 at 09:02
  • 10
    Not quite immutable. It supports `set(int index, E element)` by modifying the element in the underlying array. – Patricia Shanahan Dec 12 '13 at 09:03
  • 1
    I didn't say the elements are immutable... just the list itself. You cannot do any operation that changes the size of the list. Obviously it cannot control changes in its underlying elements. You can change an element without using set(int index, E element), just by changing its reference. – Lital Kolog Dec 12 '13 at 09:09
  • 3
    @Lital lists are immutable if and only if you cannot add/remove/ *change* element. In this case you _can_ change element just with `set` method, so this list is not immutable. The example of immutable list is the one, which you can get by `Collections.unmodifiableList` – Dmitry Ginzburg May 13 '14 at 12:40
0

I think case 2 is like creating new String[10]. You cannot change size but you can change elements. Case 1 allows you to do both.

This shows what you can do with case 2: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Arrays.java#Arrays.ArrayList

zek_w
  • 97
  • 1
  • 7