Java ArrayList Intersect Intersect(ArrayList list1, ArrayList list2)

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

Description

Finds the intersection between two lists of String objects.

License

Open Source License

Parameter

Parameter Description
list1 First list
list2 Second list

Return

Intersection list (contains values that exist in both lists)

Declaration

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

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.*;

public class Main {
    /** Finds the intersection between two lists of String objects.
     *//from  w  ww . j  ava 2s.  c om
     * @param list1 First list
     * @param list2 Second list
     * @return Intersection list (contains values that exist in both lists)
     */
    public static ArrayList<String> Intersect(ArrayList<String> list1, ArrayList<String> list2) {
        if (list1.size() == 0)
            return list2;

        Set intersection = new HashSet(list1);
        intersection.retainAll(new HashSet(list2));
        return new ArrayList(intersection);
    }
}

Related

  1. intersection(ArrayList List1, ArrayList List2)
  2. intersection(ArrayList list1, ArrayList list2)