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.
Algorithm:
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(): Guava Ints.toArray() can be used to convert set of integer to an array of integer.
Algorithm:
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(): 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.
Algorithm:
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]
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.