union list of list and return a list - Java java.util

Java examples for java.util:List Operation

Description

union list of list and return a list

Demo Code


//package com.java2s;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List collections = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(union(collections));
    }//w ww .  j  av  a2 s .com

    public static <T> List<T> union(List<List<T>> collections) {
        if (collections.size() <= 0) {
            return new LinkedList<>();
        } else if (collections.size() == 1) {
            return collections.get(0);
        } else {
            List<T> result = new LinkedList<>();
            for (List<T> list : collections) {
                result.addAll(list);
            }
            return result;
        }
    }
}

Related Tutorials