Wednesday, February 02, 2005

Compact way to create an Immutable list

List southEastStates = Arrays.asList( new String[] { "NC", "SC", "GA", "FL" });


you can create and initialize a new collection as an expression by using the "double-brace" syntax:
E.g.
 private static final Set VALID_CODES = new HashSet() {{
add("XZ13s");
add("AB21/X");
add("YYLEX");
add("AR2D");
 }};
Or:
 removeProductsWithCodeIn(new HashSet() {{
add("XZ13s");
add("AB21/X");
add("YYLEX");
add("AR5E");
 }});