Answers for "convert hashset to int java"

0

convert hashset to int java

import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Instantiating the HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Populating the HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      //Creating an empty integer array
      Integer[] array = new Integer[hashSet.size()];
      //Converting Set object to integer array
      hashSet.toArray(array);
      System.out.println(Arrays.toString(array));
   }
}
Posted by: Guest on December-27-2020
0

convert hashset to int java

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetExample {
   public static void main(String args[]) {
      //Instantiating the HashSet
      Set<Integer> hashSet = new HashSet<Integer>();
      //Populating the HashSet
      hashSet.add(1124);
      hashSet.add(3654);
      hashSet.add(7854);
      hashSet.add(9945);
      System.out.println(hashSet);
      //Creating an empty integer array
      Integer[] array = hashSet.stream().toArray(Integer[]::new);
      System.out.println(Arrays.toString(array));
   }
}
Posted by: Guest on December-27-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language