check Identical element between two List<Integer> - Java java.util

Java examples for java.util:List Compare

Description

check Identical element between two List<Integer>

Demo Code


//package com.book2s;
import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list1 = java.util.Arrays.asList("asdf", "book2s.com");
        List list2 = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(checkIdentical(list1, list2));
    }/*from   w  w w. ja  va2  s . c  om*/

    public static List<Integer> checkIdentical(List<Integer> list1,
            List<Integer> list2) {
        List<Integer> resultList = new ArrayList<Integer>();

        Iterator<Integer> iterator1 = list1.iterator();
        while (iterator1.hasNext()) {
            Integer next1 = iterator1.next();
            Iterator<Integer> iterator2 = list2.iterator();
            while (iterator2.hasNext()) {
                Integer next2 = iterator2.next();
                if (next1.intValue() == next2.intValue()) {
                    iterator1.remove();
                    iterator2.remove();
                    resultList.add(next1.intValue());
                    break;
                }
            }
        }

        return resultList;
    }
}

Related Tutorials