Posts

Showing posts from January, 2013

Union Multiple Collections into a Single Collection

As developing daily tasks at work, I realize the need of a generic method that merges given an array of collections into a single collection as stated in this StackOverflow Thread . There are reasonable references to well grounded frameworks (e.g. guava)  and sample implementations in that thread. I also check it out the popular "apache commons collections" that already offers a method that union two collections and returns a single non-modifiable collection; however I think it is not enough. There should be still some improvements to raise the bar to the next level.  As a result i define the method as follows: public static <T> Collection<T> union(Collection<T>... collections) { } Implementation is as follows, I hope it helps you... import java.util.Iterator; import java.util.Collection; /** * This class offers an alternative implementation for union an array of * (java.util.Collection) collections instead of only two collections. * http://