How to convert Array to HashSet in Java

1 Answer

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

public class MyClass {
    public static void main(String args[]) {
        String[] array = { "java", "c", "c++", "c#", "python", "php" };
        
        Set<String> hset = new HashSet<>(Arrays.asList(array));

        System.out.println(hset);
    }
}



 
/*
run:
    
[c#, c++, python, java, c, php]
  
*/

 



answered Jan 16, 2022 by avibootz

Related questions

1 answer 179 views
179 views asked Jan 16, 2022 by avibootz
1 answer 134 views
134 views asked Jul 23, 2023 by avibootz
1 answer 113 views
113 views asked Jul 22, 2023 by avibootz
1 answer 132 views
1 answer 132 views
2 answers 287 views
3 answers 121 views
121 views asked Jul 6, 2025 by avibootz
...