Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaProgram to Convert Set of Integer to Array of Integer in Java

Program to Convert Set of Integer to Array of Integer in Java

Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set to Set.

  1. Using Java 8 Stream: A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set to int[]. Algorithm:
    1. Get the set of integers.
    2. Convert Set of Integer to Stream of Integer. This is done using Set.stream().
    3. Convert Stream of Integer to IntStream.
    4. Convert IntStream to int[]. This is done using IntStream.toArray().
    5. Return/Print the integer array int[]
  2. Program: 

Java




// Java Program to convert
// Set<Integer> to int[] in Java 8
 
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
 
class GFG {
 
 // Function to convert Set of Integer to Set of String
 public static int[] convertIntSetToStringSet(
        Set<Integer> setOfInteger)
 {
  return setOfInteger.stream()
   .mapToInt(Integer::intValue)
   .toArray();
 }
 
 public static void main(String args[])
 {
  // Create a set of integers
  Set<Integer> setOfInteger = new HashSet<>(
   Arrays.asList(1, 2, 3, 4, 5));
 
  // Print the set of Integer
  System.out.println("Set of Integer: " + setOfInteger);
 
  // Convert Set of integers to set of String
  int[] intArray = convertIntSetToStringSet(setOfInteger);
 
  // Print the set of String
  System.out.println("Array of Integer: "
      + Arrays.toString(intArray));
 }
}


Output:

Set of Integer: [1, 2, 3, 4, 5]
Array of Integer: [1, 2, 3, 4, 5]

Time complexity: O(n) where n is size of setOfInteger

Auxiliary space: O(n) for intArray

  1. Using Guava Ints.toArray(): Guava Ints.toArray() can be used to convert set of integer to an array of integer. 
    1.  Algorithm:
      1. Get the set of integers
      2. Create an array of integer by Ints.toArray() method of Guava library, by passing the set of integers as the argument to this method.
      3. Return/Print the created integer array int[]
  2. Using Apache Commons toPrimitive(): Apache Commons Lang’s ArrayUtils class provides toPrimitive() method that can convert an array of object Integers to primitive ints. This set of integers needs to be converted to an array of Integers.
    1.  Algorithm:
      1. Get the set of integers
      2. Create an object of Primitive int by ArrayUtils.toPrimitive() method of Apache Commons Lang’s library
      3. Convert this primitive int to array of integer by use of toArray() method.
      4. Return/Print the created integer array int[]
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments