convert hashset to int array java

import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
class Main
{
    // Program to convert set of integer to array of int in Java
    public static void main(String args[])
    {
        Set<Integer> ints = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5));
        int[] primitive = ints.stream()
                            .mapToInt(Integer::intValue)
                            .toArray();
        System.out.println(Arrays.toString(primitive));
    }
}

3.75
4
Solublefish 110 points

                                    import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetExample {
&nbsp; &nbsp;public static void main(String args[]) {
&nbsp; &nbsp; &nbsp; //Instantiating the HashSet
&nbsp; &nbsp; &nbsp; Set&lt;Integer&gt; hashSet = new HashSet&lt;Integer&gt;();
&nbsp; &nbsp; &nbsp; //Populating the HashSet
&nbsp; &nbsp; &nbsp; hashSet.add(1124);
&nbsp; &nbsp; &nbsp; hashSet.add(3654);
&nbsp; &nbsp; &nbsp; hashSet.add(7854);
&nbsp; &nbsp; &nbsp; hashSet.add(9945);
&nbsp; &nbsp; &nbsp; System.out.println(hashSet);
&nbsp; &nbsp; &nbsp; //Creating an empty integer array
&nbsp; &nbsp; &nbsp; Integer[] array = hashSet.stream().toArray(Integer[]::new);
&nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(array));
&nbsp; &nbsp;}
}

3.75 (4 Votes)
0
0
0
Aesa 105 points

                                    import java.util.HashSet;
import java.util.Set;
public class SetExample {
&nbsp; &nbsp;public static void main(String args[]) {
&nbsp; &nbsp; &nbsp; //Instantiating the HashSet
&nbsp; &nbsp; &nbsp; Set&lt;Integer&gt; hashSet = new HashSet&lt;Integer&gt;();
&nbsp; &nbsp; &nbsp; //Populating the HashSet
&nbsp; &nbsp; &nbsp; hashSet.add(1124);
&nbsp; &nbsp; &nbsp; hashSet.add(3654);
&nbsp; &nbsp; &nbsp; hashSet.add(7854);
&nbsp; &nbsp; &nbsp; hashSet.add(9945);
&nbsp; &nbsp; &nbsp; //Creating an empty integer array
&nbsp; &nbsp; &nbsp; Integer[] array = new Integer[hashSet.size()];
&nbsp; &nbsp; &nbsp; //Converting Set object to integer array
&nbsp; &nbsp; &nbsp; hashSet.toArray(array);
&nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(array));
&nbsp; &nbsp;}
}

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source