Java ArrayList Union union(ArrayList list1, ArrayList list2)

Here you can find the source of union(ArrayList list1, ArrayList list2)

Description

union

License

Open Source License

Declaration

public static ArrayList<String> union(ArrayList<String> list1,
            ArrayList<String> list2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static ArrayList<String> union(ArrayList<String> list1,
            ArrayList<String> list2) {
        ArrayList<String> result = new ArrayList<String>();
        result.addAll(list1);//w w w.j  a  va 2s .co m
        result.addAll(list2);
        Collections.sort(result);
        return distinct(result);
    }

    public static ArrayList<String> distinct(ArrayList<String> orderedList) {
        ArrayList<String> list = new ArrayList<String>();
        if (orderedList.size() > 0) {
            list.add(orderedList.get(0));
            for (int i = 1; i < orderedList.size(); i++) {
                if (!orderedList.get(i).equals(orderedList.get(i - 1))) {
                    list.add(orderedList.get(i));
                }
            }
        }
        return list;
    }
}

Related

  1. Union(ArrayList list1, ArrayList list2)
  2. union(ArrayList a, ArrayList b)
  3. unionSets(ArrayList s1, ArrayList s2)