List subtract - Java Collection Framework

Java examples for Collection Framework:List

Description

List subtract

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static <T> List<T> subtract(List<T> list1, List<T> list2) {
        List<T> result = new ArrayList<T>();
        Set<T> set2 = new HashSet<T>(list2);
        for (T t1 : list1) {
            if (!set2.contains(t1)) {
                result.add(t1);//from w ww  . java2  s.c om
            }
        }
        return result;
    }
}

Related Tutorials