-3

What is the problem of initiating an ArrayList by:

new ArrayList<String>().add("something");

And why we should use the following to initiate an ArrayList:

new ArrayList<String>() {{ 
    add("something");
}};
Tom
  • 16,842
  • 17
  • 45
  • 54
hbak
  • 1,333
  • 3
  • 10
  • 22
  • 1
    What's wrong with this `new ArrayList.add('something')`? It won't compile ... – Tom Nov 29 '15 at 20:28
  • Thanks down voter, it seems that stackoverflow become for proficient developer only. – hbak Nov 29 '15 at 20:38
  • Maybe you would get better result with a better question? For example with code snippets which are compilable? But this is just a guess, I don't know why they voted. – Tom Nov 29 '15 at 20:54

3 Answers3

5

You should not use double brace initialization! You can but it doesn't mean you should.

To get back to your question

new ArrayList<String>().add("something"); 

The problem with this is that this actually returns a boolean: the result of the add method. The list you just instantiated is lost and will be garbage collected.

What you should do instead is keep a reference to the list and use it to add the value.

List<String> list = new ArrayList<>();
list.add("something");

// or simpler
list = Arrays.asList("something"); // warning fixed-size list
Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • But if I write "return new ArrayList().add("Something")" in a method it's returning type is List, it would causes a compilation error, since I'm return a boolean, But that is did not happened – hbak Nov 29 '15 at 20:36
  • 1
    @hbak I didn't understand your comment. `new ArrayList().add("Something")` returns a boolean value, not a List. Try to write `List list = new ArrayList().add("Something")` and you will see that there is a compilation error. – Tunaki Nov 29 '15 at 20:37
  • @hbak why would you have a method that instantiates a new list but returns a boolean? – Shiraaz.M Nov 29 '15 at 20:38
  • If I have the following declaration for a method ` List splitMultiValues` .. and the return statement for this method is `List list = (List) (x== null ? new ArrayList().add(cellValue) : y); return list;` Then what will happen? – hbak Nov 29 '15 at 20:41
  • 1
    @hbak `ClassCastException` will be thrown. – Andreas Nov 29 '15 at 20:43
  • @Andreas, actually it returns null (I don't know why) – hbak Nov 29 '15 at 20:44
  • To get a variable-size list, you can combine the two shown ways: `List list = new ArrayList<>(Arrays.asList("foo", "bar"));` – Andreas Nov 29 '15 at 20:44
  • @hbak Well that does `y` equal to? – Tunaki Nov 29 '15 at 20:45
  • it's ArrayList – hbak Nov 29 '15 at 20:45
  • @hbak If `x` is not null and `y` is null, then you get null. – Andreas Nov 29 '15 at 20:46
  • @Andreas and Tunaki. Thanks guys, I will debug the code to see what is really going on. I've done with stupid questions :) – hbak Nov 29 '15 at 20:48
  • 1
    @hbak Debugging the code sounds like a great idea indeed :) – Tunaki Nov 29 '15 at 20:48
0

The syntax you are using is wrong. You can get it working if you follow this answer:

Initialization of an ArrayList in one line

ArrayList<String> list = new ArrayList<String>();
list.add("something");
Community
  • 1
  • 1
cahen
  • 15,807
  • 13
  • 47
  • 78
0
new Array<String>().add("Something");

is not the only way to do this. You could use something like:

Arrays.asList("Something","Something Else");

This returns a List<String> though.

Shiraaz.M
  • 3,073
  • 2
  • 24
  • 40