Here you can find the source of union(ArrayList
public static ArrayList<String> union(ArrayList<String> list1, ArrayList<String> list2)
//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; } }