Java Set to Array

Java Set to Array

In this post, we will learn java set to array conversion.

There are many ways to convert set to an array.


1. Using Java 8’s Stream

If you are using Java 8, I would recommend using Java 8 Stream.

Output

[John, Martin, Mary]

2. Using toArray()

We can directly call toArray() method on set object for java set to array conversion.

Output

[John, Martin, Mary]

3. Using toArray(IntFunction) [Java 11]

You can use Set#toArray(IntFunction<T[]>) method which takes IntFunction as generator

Output

[John, Martin, Mary]

4. Using System.arraycopy()

We can use System.arraycopy() for java set to array conversion.

Output

[John, Martin, Mary]

5. Using Arrays.copyOf

We can use Arrays.copyOf() for java set to array conversion.

Here, Array.copyOf takes 3 parameters

Output

[John, Martin, Mary]

6. Using simple iteration

Iterate through set and put the value in array manually.

Output

[John, Martin, Mary]

7. Using Guava library

There are two ways to convert set to array using guava library.

7.1 Using FluentIterable

FluentIterable class provide similar functionality to Java 8’s Stream. It takes iterable set and returns an array which contains all the elements from fluent iterable.

Output

[John, Martin, Mary]

7.2 Using Iterables

Iterables class provides toArray method which takes set and newType as input and return array contains iterable’s elements.

Output

[John, Martin, Mary]

That’s all about Java set to array.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

  • Convert String to Array in java
    11 December

    Convert String to Array in Java

    Table of ContentsHow to convert String to Array in JavaUsing toArray() method of SetUsing the split() method of String classUsing StringTokenizor classUsing the split() method of StringUtils classUsing split() method of Pattern classConclusion When developing applications in Java there are many cases where we will find ourselves converting data from one type to the other. […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.Date In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.