Dave's Brain

Browse - Programming Tips - How can I best convert a Java array into an HashSet?

Date: 2016jan22
Language: Java

Q.  How can I best convert a Java array into an HashSet?

A.  Use Arrays.asList() in the HashSet constructor, like this:

String [] a = { "apple", "snapple", "crackle" }; HashSet<String> choices = new HashSet<String>(Arrays.asList(a));

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.