Reputation: 1171
I am generating an ArrayList of objects. Following is the code
ArrayList someArrayList = new ArrayList();
Public ArrayList getLotOfData()
{
ArrayList someData = new ArrayList();
return someData;
}
someArrayList = eDAO.getLotOfData();
Once I have this ArrayList object "someArrayList", I would like to declare it public and final and store it in a Constants file so that it can be accessed globally. Is there a way I can do that? If I declare an Arraylist object public and final, then I would not be able to reassign any values to it. I tried the following
public final ArrayList anotherArrayList = new ArrayList();
anotherArrayList.addAll(someArrayList);
I had hoped to store this "anotherArrayList" as a global ArrayList object and use it, but this returns a nullpointer exception. I want to use it just like a String constant "ConstantsFile.anotherArrayList". Any ideas???
Upvotes: 54
Views: 149819
Reputation: 483
Since Java 9 the solution is super simple.
You do not even need Guava, since we have:
public static final List<String> list = List.of("foo", "bar");
Upvotes: 3
Reputation: 951
Here is another way you can create a final ArrayList and then make it unmodifiable. It involves creating a class to hold the final ArrayList.
First, make your list, doing all the mutations you need to do.
Second, create a class that will create the final ArrayList in its constructor.
public class Wrapper {
final List<T> list;
// Other final variables to be set and added to the constructor.
public Wrapper(List<T> list) {
this.list = Collections.unmodifiableList(list);
}
}
This is useful for when you need an immutable class with a List.
Upvotes: 0
Reputation: 198571
Guava provides ImmutableList
for just about this reason. (Also, it doesn't have the unnecessary space overhead that ArrayList
allocates to make room for future elements which you won't be adding for your application.)
public static final ImmutableList<String> CONSTANTS =
ImmutableList.of("foo", "bar");
Upvotes: 37
Reputation: 14257
Java 1.4 compatible way:
public static final List STRINGS = Collections.unmodifiableList(
Arrays.asList(new String[] {"foo", "bar"}));
Such List is unmodifiable, calling methods such as add()
, remove()
or set()
will cause UnsupportedOperationException
.
For less ancient Java versions:
public static final List<String> STRINGS = Collections.unmodifiableList(
Arrays.asList("foo", "bar"));
And finally, Java 9 comes with:
public static final List<String> STRINGS = List.of("foo", "bar");
Upvotes: 32
Reputation: 425448
You can easily make it public static final
, but that won't stop people from changing the contents.
The best approach is to safely publish the "constant" by:
Resulting in one neat final declaration with initialization:
public static final List<String> list = Collections.unmodifiableList(
new ArrayList<String>() {{
add("foo");
add("bar");
// etc
}});
or, similar but different style for simple elements (that don't need code)
public static final List<String> list =
Collections.unmodifiableList(Arrays.asList("foo", "bar"));
Upvotes: 101