2

In my C# project I have a static List that gets filled immediately when declared.

  private static List<String> inputs = new List<String>()
        { "Foo", "Bar", "Foo2", "Bar2"};

How would I do this in Java using the ArrayList?

I need to be able to access the values without creating a instance of the class. Is it possible?

1
  • 4
    (You probably want to make the field final.) Commented Jan 12, 2010 at 21:28

5 Answers 5

16

You can use Double Brace Initialization. It looks like:

private static List<String> inputs = new ArrayList<String>()
  {{ add("Foo");
    add("Bar");
    add("Foo2");
    add("Bar2");
  }};
Sign up to request clarification or add additional context in comments.

2 Comments

IMO This is the best answer, since it doesn't rely on helper class java.util.Arrays.
But it does create a somewhat hidden anonymous inner class. Subjective call on whether or not that is a better tradeoff.
8

I don't understand what you mean by

able to access the values without creating a instance of the class

but the following snippet of code in Java has pretty much the same effect in Java as yours:

private static List<String> inputs = Arrays.asList("Foo", "Bar", "Foo2", "Bar2");

4 Comments

As it is a static field, it really should be immutable. Unfortunately this adds to the verbosity. private static final List<String> inputs = Collections.unmodifiableList(Arrays.asList("Foo", "Bar", "Foo2", "Bar2"));. Should get list literals in JDK7.
Yay for the less verbose, later answer winning out? (No offense to you MAK. Just stating my perceived truth.)
@jdmichal: I think your answer was posted while I was still writing mine - otherwise I wouldn't have bothered to say the same thing again. I guess the slowest gun won :).
I often like to run new ArrayList<>(Arrays.asList(...)) because Arrays.asList() doesn't produce a full implementation of List but ArrayList does. It's slightly slower if this has to be done repeatedly but if it's a one-shot deal then it's fine.
7

You can make static calls by enclosing them within static{} brackets like such:

private static final List<String> inputs = new ArrayList<String>();

static {
  inputs.add("Foo");
  inputs.add("Bar");
  inputs.add("Foo2");
  inputs.add("Bar2");
}

1 Comment

That's nice, I didn't know that.
5

Do you need this to be an ArrayList specifically, or just a list?

Former:

private static java.util.List<String> inputs = new java.util.ArrayList<String>(
    java.util.Arrays.<String>asList("Foo", "Bar", "Foo2", "Bar2"));

Latter:

private static java.util.List<String> inputs =
    java.util.Arrays.<String>asList("Foo", "Bar", "Foo2", "Bar2");

java.util.Arrays#asList(...) API

Comments

3

You may enjoy ImmutableList from Guava:

ImmutableList<String> inputs = ImmutableList.of("Foo", "Bar", "Foo2", "Bar2");

The first half of this youtube video discusses the immutable collections in great detail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.