Java ArrayList Intersect intersection(ArrayList list1, ArrayList list2)

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

Description

intersection

License

Open Source License

Declaration

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

Method Source Code

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

import java.util.ArrayList;

public class Main {
    public static ArrayList<String> intersection(ArrayList<String> list1,
            ArrayList<String> list2) {
        ArrayList<String> result = new ArrayList<String>();
        int i1 = 0;
        int i2 = 0;
        while (i1 < list1.size() && i2 < list2.size()) {
            String s1 = list1.get(i1);
            String s2 = list2.get(i2);
            int num = s1.compareTo(s2);
            if (num == 0) {
                result.add(s1);// ww  w. j  a  va 2s  .c om
                i1++;
                i2++;
            } else if (num < 0) {
                i1++;
            } else { // num > 0
                i2++;
            }
        }
        return result;
    }
}

Related

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