Converting an Array to a Collection

Sometimes the common problems seems to be we don’t remember the API, one such scenario I recently faced was how to convert a Array to a collection? Following seems to decent ways :-)

// Fixed-size list
List list = Arrays.asList(array);

// Growable list
list = new LinkedList(Arrays.asList(array));

// Duplicate elements are discarded
Set set = new HashSet(Arrays.asList(array));

Written by Ravi Nallakukkala on November 29th, 2007 with no comments.
Read more articles on Java/ J2EE.

Related articles

No comments

There are still no comments on this article.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> .