Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating prepopulated set in Java [duplicate]

Tags:

In Java, how do I create a final Set that's populated at construction? I want to do something like the following:

static final Set<Integer> NECESSARY_PERMISSIONS      = new HashSet<Integer>([1,2,3,6]); 

but I don't know the proper syntax in Java.

like image 231
Sal Avatar asked Feb 27 '12 18:02

Sal


People also ask

How do you create a static set in Java?

When initializing static final sets I usually write it like this: public static final String[] SET_VALUES = new String[] { "a", "b" }; public static final Set<String> MY_SET = new HashSet<>(Arrays. asList(SET_VALUES));


1 Answers

Try this idiom:

import java.util.Arrays;  new HashSet<Integer>(Arrays.asList(1, 2, 3, 6)) 
like image 192
Tomasz Nurkiewicz Avatar answered Oct 21 '22 10:10

Tomasz Nurkiewicz