How to add an int array to HashSet in Java

1 Answer

0 votes
import java.util.HashSet;

public class MyClass {
    public static void main(String args[]) {
      	int array[] = {2, 3, 6, 5, 30, 0};
      	
      	HashSet<Integer> hset = new HashSet<>(); 
      	
      	for (int element : array) {
            hset.add(element);
        }

	    System.out.println(hset);
    }
}





/*
run:
 
[0, 2, 3, 5, 6, 30]
 
*/

 



answered Jul 22, 2023 by avibootz

Related questions

1 answer 121 views
1 answer 119 views
119 views asked Jul 23, 2023 by avibootz
1 answer 182 views
182 views asked Feb 16, 2021 by avibootz
1 answer 206 views
1 answer 192 views
1 answer 122 views
2 answers 269 views
...