Wednesday 27 May 2015

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]

1 comment:

  1. This line seems to incorrect(asLIST):
    Set hs = new HashSet(Arrays.asList(arr));


    ReplyDelete