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 268 views
268 views asked Jan 16, 2022 by avibootz
1 answer 222 views
222 views asked Jul 23, 2023 by avibootz
1 answer 192 views
192 views asked Jul 22, 2023 by avibootz
1 answer 212 views
1 answer 213 views
2 answers 392 views
3 answers 254 views
254 views asked Jul 6, 2025 by avibootz
...