Java List Intersect intersection(final List list1, final List list2)

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

Description

intersection

License

Apache License

Declaration

public static <T> List<T> intersection(final List<? extends T> list1, final List<? extends T> list2) 

Method Source Code


//package com.java2s;
/*//from w ww . jav a  2 s . co m
 * Commons-Utils
 * Copyright (c) 2017.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

import java.util.*;

public class Main {

    public static <T> List<T> intersection(final List<? extends T> list1, final List<? extends T> list2) {
        List<? extends T> smaller = list1;
        List<? extends T> larger = list2;
        if (list1.size() > list2.size()) {
            smaller = list2;
            larger = list1;
        }

        List<T> newSmaller = new ArrayList<T>(smaller);
        List<T> result = new ArrayList<T>(smaller.size());
        for (final T e : larger) {
            if (newSmaller.contains(e)) {
                result.add(e);
                newSmaller.remove(e);
            }
        }
        return result;
    }
}

Related

  1. intersect2orderedList(List s1, List s2)
  2. intersectAll(final List collector, final T[] a, final T[] b)
  3. intersectDate(List oDate, List tDate)
  4. intersection(final List list1, final List list2)
  5. intersection(final List list1, final List list2)
  6. intersection(List> sets)
  7. intersection(List list1, List list2)
  8. intersection(List list1, List list2)
  9. Intersection(List a, List b)