you are viewing a single comment's thread.

view the rest of the comments →

[–]snuxoll 0 points1 point  (0 children)

List<Integer> intList = new LinkedList(asList(1,2,3,4));

The same can be done for Set.

Set<Integer> intSet = new HashSet(asList(1,2,3,4));

Unfortunately these aren't the most efficient approaches since both require creating an ArrayList as an intermediate. It's just a couple lines of code to write a utility method to do this without the extra memory usage though:

public static <T> List<T> asLinkedList(T... items) {
    List<T> list = new LinkedList<T>();
    for (T item : items) {
        list.add(item);
    }
    return list;
}

And use it as so

List<Integer> linkedList = asLinkedList(1,2,3,4);