Java Tips: Initializing Collection
Especially in unit test, we have to initialize an array or a collection. Well, for array, it’s OK… A simple code that we know can solve the problem:
String[] s = new String [] {"1", "2"};
But how about Collection? Normal way to initialize collection is something like this (which pretty ugly):
List<String> s = new ArrayList<String>(); s.add("1"); s.add("2");
I hardly find an elegant solution until I see this post. There are at least three better solution for the case.
First solution:
List<String> s = new ArrayList<String>() {{ add("1"); add("2"); }};
Which unfortunately, doesn’t pass Java Code Convention (that is, if you format the code, it will become uglier than the original).
List<String> s = new ArrayList<String>() { { add("1"); add("2"); } };
Second solution:
List<String> s = Arrays.asList(new String[]{"1", "2"});
This solution is the best if you use Java 1.4 or before. But if you use Java 5, the third is more elegant:
List<String> s = Arrays.asList("1", "2");
Great!
EDIT: Rob Juurlink commented that this solution will create a fixed read only collection, so you may want to wrap it with ArrayList (or other Collection class) to make it writable
13 Comments »
RSS feed for comments on this post. TrackBack URL
[...] is not that advance. I expect that many have already known this and I even have already write a tips of one nice application of this technique. For those who, Java 5 provides a nicer way to define an array [...]
I like this trick:
List list = Arrays.asList( “1,2,3,4,5,6,7″.split(”,”) );
Hi Christian… I like your idea, but IMO is still worst than the last suggestion and it only works for String with special character as denominator.
Anyway, thank you for stopping by
But the returned list is fixed size readonly.
To fix that, Arrays.asList() has to be wrapped in a new ArrayList constructor, but then it is more ugly again:
List s = new ArrayList(Arrays.asList(”1″, “2″));
You are correct here… But fortunately this never been a problem in most of the cases (at least for my situation)
Hi, you should really take a look at the Google Collections API. There they try to solve these kind of things.
http://code.google.com/p/google-collections/
Google always creates good product. I certainly will try to use it if there is no limitation what library should I use. Unfortunately, most of the times, I’m tied to certain libraries
check out this code
JButton button = new JButton();
button.setText(”hello”);
button.setRolloverEnabled(true);
button.setOpaque(true);
JButton anotherButton = new JButton() {{
setText(”hello”);
setRolloverEnabled(true);
setOpaque(true);
}};
I think this way of coding improve code readibility. Please comment.
@CK: Yes, I agree with you for your example. But for Collection, for me the last solution is better (at least if you don’t need performance and fixed read only array is okay for you)
Nanda Firdausi ,
Good post.enjoyed reading it.
Thanks
Prashant Jalasutram
http://prashantjalasutram.blogspot.com/
I like to have helper methods such as
List<Integer> list = newList(1,2,3,4,5,6,7);
and
Set<String> set = newSetSplit(”aye, bee, see, dee, ee, eff, gee”, “, ?”);
and
Map<String, Integer> map = newMap(”hello”, 1, “there”, 2);
Hey thanks man….
That was quite helpful!
I always get confused with the collections in java.
Thanks again.
[...] time ago I wrote an article about initializing collection in Java. I just found that Java actually provides several methods to help this task if you just want to [...]