Browse - Programming Tips - How do I initialize a HashSet?

Date: 2012sep23
Language: Java
Keywords: init

Q.  How do I initialize a HashSet?

A.  Like this:

HashSet<String> shopping_list = new HashSet<String>() {{ add("milk"); add("peanut butter"); add("bread"); }}; // Notice the double bracebrackets