18

I have a very simple question:

How can I make this code more simple on Java:

ArrayList<String> s = new ArrayList<String>();
s.add("str1");
s.add("str hello");
s.add("str bye");
//...

Something like that:

ArrayList<String> s = {"a1", "str", "mystr"};

or that:

ArrayList<String> s = new ArrayList<String>("a1", "str", "mystr");

or that:

ArrayList<String> s = new ArrayList<String>();
s.addAll("a1", "str", "mystr");

or that:

ArrayList<String> s = new ArrayList<String>();
s.addAll(new ArrayElements("a1", "str", "mystr"));

I just want syntax hint. Thanks.

pleerock
  • 18,322
  • 16
  • 103
  • 128

7 Answers7

41

List<String> s = Arrays.asList("a1", "str", "mystr");

dty
  • 18,795
  • 6
  • 56
  • 82
  • 8
    This returns a fixed-size list not a java.util.ArrayList. So it is good when no more adding or removing of elements is needed. – x22 Oct 14 '11 at 13:03
  • 3
    This is true. It wasn't clear from the OP whether this would be an issue or not. – dty Oct 14 '11 at 13:49
25

How about:

ArrayList<String> s = new ArrayList<String>();
Collections.addAll(s, "a1", "str", "mystr");
NPE
  • 486,780
  • 108
  • 951
  • 1,012
5
List<String> s = Arrays.asList(new String[] {"a1", "str", "mystr"});
solendil
  • 8,432
  • 4
  • 28
  • 29
5

I would use Guava and its wonderful Lists class:

List<String> list = Lists.newArrayList("a1", "str", "mystr");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

You can use double brace:

ArrayList<String> s = new ArrayList<String>()
{{
    add("str1");
    add("str hello");
    add("str bye");
    //...
}};
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • 2
    Rather than just producing esoteric syntax, wouldn't it be a good idea to explain to the OP what that is and how it works? – dty Oct 14 '11 at 12:26
  • 1
    dty, it is double brace initialization, it explained in http://c2.com/cgi/wiki?DoubleBraceInitialization in detail. – SerCe Sep 03 '14 at 17:03
  • There are reasons why you might not want to do this. This essentially creates a new anonymous inner class definition every time it's run, which can have side effects on a long running app, or if you execute this snippet frequently. It's often better to not use this form of initialization, because even if you understand the implications, someone on your team who may copy you may not. There are safer ways to do this – Julian May 29 '18 at 17:51
2

Before or after Java 8 you can write:

ArrayList<String> s = 
    new ArrayList<>(Arrays.asList("str1", "str hello", "str bye"));

Since Java 8 you can write:

ArrayList<String> s =
    Stream.of("str1", "str hello", "str bye")
        .collect(Collectors.toCollection(ArrayList::new));

With Eclipse Collections you can write the following:

ArrayList<String> s =
    Lists.mutable.with("str1", "str hello", "str bye")
        .into(new ArrayList<>());

If you can use List instead of ArrayList:

List<String> s = Lists.mutable.with("str1", "str hello", "str bye");

Note: I am a committer for Eclipse Collections.

Donald Raab
  • 6,458
  • 2
  • 36
  • 44
0

If you're using Java 9, you could use List.of() :

List<String> s = List.of("str1", "str hello", "str bye");

But this would be an immutable list. If you need a mutable Arraylist:

ArrayList<String> s = new ArrayList<>(List.of("str1", "str hello", "str bye"))
Neeraj
  • 2,376
  • 2
  • 24
  • 41