HashSet<String> shopping_list = new HashSet<String>() {{ add("milk"); add("peanut butter"); add("bread"); }}; // Notice the double bracebracketsSome people don't like the double brace brackets because it creates an anonymous inner class. But I like it because its compact.
Programming Tips - Java: How to initialize a HashSet
Date: 2012sep23
Update: 2025jul13
Language: Java
Keywords: init
Q. Java: How to initialize a HashSet
A. Here is a nice way to do it: