Skip to main content
deleted 12 characters in body
Source Link
Gennadiy
  • 18.3k
  • 4
  • 28
  • 21

There is a shorthand that I use that is not very time efficient, but fits on a single line:

Set<String> h = new HashSet<String>HashSet<>(Arrays.asList("a", "b"));

Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set.

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<String>HashSet<>(Arrays.asList(SET_VALUES));

Slightly less ugly and efficiency does not matter for the static initialization.

There is a shorthand that I use that is not very time efficient, but fits on a single line:

Set<String> h = new HashSet<String>(Arrays.asList("a", "b"));

Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set.

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<String>(Arrays.asList(SET_VALUES));

Slightly less ugly and efficiency does not matter for the static initialization.

There is a shorthand that I use that is not very time efficient, but fits on a single line:

Set<String> h = new HashSet<>(Arrays.asList("a", "b"));

Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set.

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));

Slightly less ugly and efficiency does not matter for the static initialization.

Removing the String array declaration since it's not needed as Jason Nichols pointed out in his answer below.
Source Link

There is a shorthand that I use that is not very time efficient, but fits on a single line:

Set<String> h = new HashSet<String>(Arrays.asList(new String[] { "a", "b" }));

Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set.

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<String>(Arrays.asList(SET_VALUES));

Slightly less ugly and efficiency does not matter for the static initialization.

There is a shorthand that I use that is not very time efficient, but fits on a single line:

Set<String> h = new HashSet<String>(Arrays.asList(new String[] { "a", "b" }));

Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set.

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<String>(Arrays.asList(SET_VALUES));

Slightly less ugly and efficiency does not matter for the static initialization.

There is a shorthand that I use that is not very time efficient, but fits on a single line:

Set<String> h = new HashSet<String>(Arrays.asList("a", "b"));

Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set.

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<String>(Arrays.asList(SET_VALUES));

Slightly less ugly and efficiency does not matter for the static initialization.

Source Link
Gennadiy
  • 18.3k
  • 4
  • 28
  • 21

There is a shorthand that I use that is not very time efficient, but fits on a single line:

Set<String> h = new HashSet<String>(Arrays.asList(new String[] { "a", "b" }));

Again, this is not time efficient since you are constructing an array, converting to a list and using that list to create a set.

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<String>(Arrays.asList(SET_VALUES));

Slightly less ugly and efficiency does not matter for the static initialization.