How to convert array to HashSet

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ArrayToHashSet {
public static void main(String[] args) {

String[] arr = { “one”, “two”, “third” };
Set<String> hs = new HashSet<String>(Arrays.asList(arr));

// print hashset

System.out.println(“hashset is =” + hs);

}
}

Output:

hashset is =[two, one, third]

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 43 = 50