Languages
[Edit]
EN

Java - initialize HashSet at construction

0 points
Created by:
kaya31
526

In this article, we would like to show you how to initialize HashSet at the construction in Java.

Quick solution:

 

1. Initialize HashSet using Arrays.asList()

Edit

In this example, to initialize letters HashSet at the construction time, we use Arrays.asList() method in the following way:

Output:

Note:

This approache is inefficient since it needs to create an array, convert it to a list, and use it to create a HashSet.

2. Initialize HashSet using anonymous class

Edit

In this example, we use double curly braces to initialize letters HashSet on its creation.

Output:

Note:

This approache is inefficient since it needs to create anonymous class each time it's called

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join