How to initialize HashSet values by construction?
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.
How to initialize HashSetLong with values
The array values should be long, you need to specify explicitly.
Just append l/L to the values inside Arrays.asList(xxxl, xxl, xl);
How to Initialize Values to a HashSetString[,] in C#
tblNames.Add(new [,] { { "0", "tblAssetCategory" }});
Creating prepopulated set in Java
Try this idiom:
import java.util.Arrays;
new HashSet<Integer>(Arrays.asList(1, 2, 3, 6))
Does a collection initialize set the initial size of a HashSet properly?
TL;DR; Don't use a collection initializer, if you have a pre-existing array or collection.
The collection initializer just uses Add()
to create the collection, so yes, it is slightly better for performance to set the size first. It's unlikely you will actually notice any difference in most cases though.
The code for the relevant HashSet constructor has this little gem:
public HashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
{
...........
// to avoid excess resizes, first set size based on collection's count. Collection
// may contain duplicates, so call TrimExcess if resulting hashset is larger than
// threshold
ICollection<T> coll = collection as ICollection<T>;
int suggestedCapacity = coll == null ? 0 : coll.Count;
Initialize(suggestedCapacity);
this.UnionWith(collection);
So if you have an existing array (which is an ICollection<T>
, as is a List<T>
) then it won't matter. It's only an issue when using LINQ Where
etc, or when using collection initializers.
How to initialize HashSet variable during debug in eclipse?
If you want to use a class that is not imported (no corresponding import
statement in the current file), then you can use these classes by their fully qualified names:
new java.util.HashSet<String>(java.util.Arrays.asList(new String[]{"a", "b"}));
Related Topics
How to Use @Id with String Type in JPA/Hibernate
Use Custom Fonts When Creating PDF Using Ireport
Jschexception: Algorithm Negotiation Fail
How to Insert Image into Jtable Cell
Spring Resttemplate Get with Parameters
Choose Between Executorservice's Submit and Executorservice's Execute
Get an Outputstream into a String
How to Determine If a Number Is a Prime with Regex
Retrieving a List from a Java.Util.Stream.Stream in Java 8
Getting Max Value from an Arraylist of Objects
MAC Osx - Illegalstateexception: the Driver Is Not Executable:
How to Set Icon to a Jlabel from an Image from a Folder
Pass String as Params from One Java App to Another
Cannot Resolve Constructor Firefoxdriver(Org.Openqa.Selenium.Firefox.Firefoxprofile)