Java Array to Set

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

There are many ways to convert array to set.


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 HashSet constructor()

We can directly call HashSet‘s constructor for java set to array conversion.

Output

[John, Martin, Mary]

3. Using Google Guava()

We can use google guava library to do Array to Set conversion using Sets’s newHashSet method.

Output

[John, Martin, Mary]

That’s all about Java Array to set conversion.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *