DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Sets.newLinkedHashSet(E... elements) helper method #1706

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be good to add a vararg helper method for creating LinkedHashSet<E> 
similar to the one that already exists for HashSet<E>.

Original issue reported on code.google.com by NikolayM...@gmail.com on 26 Mar 2014 at 4:08

GoogleCodeExporter commented 9 years ago
If you're using Java7+, you're best bet is to use the diamond operator and 
Arrays.asList():

import static java.util.Arrays.asList;
...
import java.util.LinkedHashSet;
import java.util.Set;
...
Set<Element> mySet = new LinkedHashSet<>(asList(e1, e2, e3, e4, ...));

In fact, we'll probably be removing most of the static factory methods from 
Lists/Sets/Maps at some point in the future.

Original comment by kak@google.com on 26 Mar 2014 at 4:17

GoogleCodeExporter commented 9 years ago
Don't forget: the most common fix is just to switch to ImmutableSet.of(e1, e2, 
...).

Original comment by kevinb@google.com on 26 Mar 2014 at 4:21

GoogleCodeExporter commented 9 years ago
It seems to me the whole point of those static methods is to reduce boilerplate 
noise. Surely you don't think the code in your comment is neater than:
Sets.newLinkedHashSet(e1,e2,e3, ...)

There is also the fact that Arrays.asList creates a whole new object which 
Sets.newHashSet avoids.

Original comment by NikolayM...@gmail.com on 26 Mar 2014 at 4:24

GoogleCodeExporter commented 9 years ago
I hadn't thought of ImmutableSet. Thanks kevinb.

Original comment by NikolayM...@gmail.com on 26 Mar 2014 at 4:26

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:09

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07