Dave's Brain

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

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.