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 126 views
1 answer 127 views
127 views asked Jul 23, 2023 by avibootz
1 answer 194 views
194 views asked Feb 16, 2021 by avibootz
1 answer 212 views
1 answer 206 views
1 answer 128 views
2 answers 276 views
...