How to convert values to HashSet directly | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to convert values to HashSet directly

I'm making a constructor that takes a HashSet as a parameter. How would you pass values directly on the constructor? E.g. ``` class A { A(int x, HashSet<int> y) {} } A a = new A(5, /*how to pass 6 and 7 directly?*/); ```

6th Sep 2019, 12:48 PM
Kulisap
Kulisap - avatar
1 Answer
+ 1
~ swim ~ (not available on messanger) Thanks for making it clear that you can only pass objects in a HashSet. I didn't know that. Actually I've found another answer. A a = new A(5, new HashSet<>(Arrays.asList(6, 7))); My goal was just to make it so that you don't have to declare a collection. Thanks for your answer
6th Sep 2019, 5:56 PM
Kulisap
Kulisap - avatar