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.
Steps to convert Java array to Collection,Set and List in Java
As I said, it's a cakewalk, once you know about Arrays.asList() and how to use it, you are done. Later you can use different conversion constructor provided by Collection class to convert one collection to other e.g. List to Set and so on.
Array to Collection in Java
1) Convert Array to List using Arrays.asList() method
2) Store that as Collection
Array to Set in Java
1) Convert Array to List
2) Create Set by copying objects form List
Array to List in Java
1) Arrays.asList() returns a List, so no further conversion.
If you follow this blog, then you might remember that, we have already seen use of Arrays.asList() method in earlier article, How to create and initialize List in one line, and we have also discussed there that List returned by Arrays.asList() method is a fixed length list, which means it doesn't support add() and remove() method. If you try to add or remove any object from this List, you will get "Exception in thread "main" java.lang.UnsupportedOperationException". By the way, it's worth knowing that this List is not a read only List in Java, as you can still update values by using set(index) method.
Java Program to Convert Array to List and Set in Java
Here, I have used SL4J over Log4j for logging, which means you either need to convert log statement to System.out.println statements, or you need to include following dependency in your Maven project's pom.xml or log4j-1.2.16.jar, slf4j-api-1.6.1.jar and slf4j-log4j12-1.6.1.jar in your Java program's classpath.
That's all on How to convert an array to Collection in Java. We have seen examples of converting both String and Integer array to Collection, List, and Set in Java. You can same technique and same method Arrays.asList() to convert any Array to Collection in Java. Remember, when you create List from array, you will get elements in same order, as they are currently in array, but when you convert them to Set, you lose any ordering guarantee.
Related Java tutorials to learn Collection Framework in Java