Add all items in an array to a collection

So I wanted to add all the items in an array to a collection in Java easily. A look into the util package documentation offered a solution which I now share.

yourCollection.addAll(java.util.Arrays.asList(yourArray));

The collection can be any object from the collections framework (List, Set, Queue) and yourArray is the array with the elements to be added.
If you are using generics, the component type of the array has to be a assignment compatible with the type of the collection. In other words, if your list is something like List<CharSequence>, then you can add an array of type StringBuffer[], String[], or CharSequence[], but not Long[].

Comments