http://www.c2.com/cgi/wiki?DoubleBraceInitialization
To pass a set to a method:
To pass a set to a method:
Set<String> validCodes = new HashSet<String>(); validCodes.add("XZ13s"); validCodes.add("AB21/X"); validCodes.add("YYLEX"); validCodes.add("AR2D"); removeProductsWithCodeIn(validCodes);Or to initialize a set of constants:
private static final Set<String> VALID_CODES = new HashSet<String>(); static { validCodes.add("XZ13s"); validCodes.add("AB21/X"); validCodes.add("YYLEX"); validCodes.add("AR2D"); }But... you can create and initialize a new collection as an expression by using the "double-brace" syntax: E.g.
private static final Set<String> VALID_CODES = new HashSet<String>() {{ add("XZ13s"); add("AB21/X"); add("YYLEX"); add("AR2D"); }};Or:
removeProductsWithCodeIn(new HashSet<String>() {{ add("XZ13s"); add("AB21/X"); add("YYLEX"); add("AR5E"); }});
No comments:
Post a Comment