union Two List - Java java.util

Java examples for java.util:List Operation

Description

union Two List

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        List la = java.util.Arrays.asList("asdf", "java2s.com");
        List lb = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(unionList(la, lb));
    }/* www.j  a  v  a  2 s .  c o m*/

    public static <T> List<T> unionList(List<T> la, List<T> lb) {
        if (la == null) {
            la = new ArrayList<T>();
        }
        if (lb == null) {
            lb = new ArrayList<T>();
        }
        List<T> cpLa = new ArrayList<T>();
        List<T> cpLb = new ArrayList<T>();
        cpLa.addAll(la);
        cpLb.addAll(lb);

        List<T> dumpList = new ArrayList<T>();
        for (T t : cpLa) {
            if (cpLb.contains(t)) {
                dumpList.add(t);
            }
        }
        cpLa.removeAll(dumpList);
        cpLb.removeAll(dumpList);
        cpLa.addAll(cpLb);
        return cpLa;
    }
}

Related Tutorials