Java program to convert Set of Integer to Array of Integer

Java Set may be a a part of java.util package and extends java.util.Collection interface. It doesn't allow the utilization of duplicate elements and at max can accommodate just one null element.

A Stream may be a sequence of objects that supports various methods which may be pipelined to supply the specified result. Java 8 Stream API are often wont to convert Set to line .

1. Using Java 8 Stream : 

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[]

Program:

// Java Program to convert
// Set 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 setOfInteger)
	{
		return setOfInteger.stream()
			.mapToInt(Integer::intValue)
			.toArray();
	}

	public static void main(String args[])
	{
		// Create a set of integers
		Set 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]

2. Using Guava Ints.toArray()

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[]

Program:

 
// Java Program to convert
// Set to int[] in Java 8

import com.google.common.primitives.Ints;
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 setOfInteger)
	{
		return Ints.toArray(setOfInteger);
	}

	public static void main(String args[])
	{
		// Create a set of integers
		Set 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]

3. Using Apache Commons toPrimitive(): The ArrayUtils class of Apache Commons Lang provides the toPrimitive() method, which can convert an array of object integers to primitive integers. This set of integers needs to be converted to an array of integers.

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[]

Program

// Java Program to convert
// Set to int[] in Java 8

import org.apache.commons.lang.ArrayUtils;
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 setOfInteger)
	{
		return ArrayUtils
			.toPrimitive(setOfInteger
							.toArray(new Integer[0]));
	}

	public static void main(String args[])
	{
		// Create a set of integers
		Set 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]

 

Submit Your Programming Assignment Details