Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Convert integer set to int array using Java 8

  • How to convert Integer Set to primitive int array.
  • By Using java 8 Streams we can convert Set to array.
  • set.stream().mapToInt(Number::intValue).toArray();
  • Lets see an example java program on how to convert integer set to int array using java 8

#1: Java Example program on converting Integer Set to int Array

  1. package com.instanceofjava;

  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.Set;

  5. public class SetToArray {
  6. /**
  7.  * @author www.Instanceofjava.com
  8.  * @category interview programming questions
  9.  * 
  10.  * Description: convert Integer set to int array using java 8
  11.  *
  12.  */
  13. public static void main(String[] args) {
  14. Set hashset= new HashSet(Arrays.asList(12,34,56,78,99));
  15. int[] array = hashset.stream().mapToInt(Number::intValue).toArray();
  16. for (int i : array) {
  17. System.out.println(i);
  18. }

  19. }

  20. }

Output:

  1. 34
  2. 99
  3. 56
  4. 12
  5. 78










This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

How to Convert integer set to int array using Java 8

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×