3 ms·
My preferred initialization for lists of strings (shorter than the anon class + instance init e.g.) List<String> fruits = Arrays.asList("apple,orange,pear".s
by 6ren 15y ago
My preferred initialization for lists of strings (shorter than the anon class + instance init e.g.)
List<String> fruits = Arrays.asList("apple,orange,pear".split(","));
- chrisbroadfoot 15y agoWhy not Arrays.asList("apple", "orange", "pear")
- 6ren 15y agoYou save "" for each extra item, which gets annoying on longer lists.
- eneveu 15y agoI personally prefer Guava's immutable collections: List<String> fruits = ImmutableList.of("apple", "orange", "pear"); It also works for sets and maps: Set<String> fruits = ImmutableSet.of("apple", "orange", "pear"); Map<String, Integer> fruitCounts = ImmutableMap.of("apple", 3, "orange", 2, "pear", 6); http://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained http://code.google.com/p/guava-libraries/wiki/ImmutableColle...