In this example we will learn how to convert an String array to
Collection, Set or List in Java.
This knowledge of converting array to
Collection can be really useful to any Java developer, as most of legacy
code tend to use array, which means you either need to pass them your input as
array or they return result as array. Since newer Java code prefer Collection over array, which they
should, because of flexibility offered by Collection classes,
we often need to convert Array into different Collection classes e.g. List, Set or simply
Collection. I have shown couple of techniques for converting
array to arraylist, which equally applicable, when it comes to convert
Array to List in Java. In this article, we will go couple of steps further and
not only learn converting array to List, but also array to Set and array
to Collection in Java. Well, it's only one method, which you need to know, Arrays.asList(), which
accepts an array and return a List, later you can convert this List into any
other Collection, by using copy
constructor provided by Collection classes.