Find your content:

Search form

You are here

Convert Comma Separated String to HashSet in Java

 
Share

Example program given below.

String str = "1,2,3,4,2,5";
        
        //split the string by comma
        String[] strParts = str.split(",");
        
        //convert array to the List using asList method
        List<String> listParts = Arrays.asList(strParts);
        
        //create HashSet from the List using constructor
        HashSet<String> hsetFromString = new HashSet<String>( listParts );
        
        System.out.println("HashSet contains: " + hsetFromString);

My Block Status

My Block Content