Java List Intersect intersection_type(List list1, List list2)

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

Description

This will determine the intersection type of two list Note: undirected 1 vs 2

License

Open Source License

Parameter

Parameter Description
list1 a parameter
list2 Type 1: total 1,2,3 vs 1,2,3 Type 2: partial 1,2,3 vs 1,2,3,4,5 -> Type 5: partial 1,2,3,4,5 vs 1,2,3 <- Type 3: partial 1,2,3 vs 2,3,4 Type 4: disjoint 1,2,3 vs 4,5,6

Declaration

public static <T> int intersection_type(List<T> list1, List<T> list2) 

Method Source Code


//package com.java2s;
/*//  w  w w . ja va 2 s.  co  m
 *  COMPONENT-GRAPHER v1.0
 *  
 *  Copyright (C) 2015-2016  Etienne Lord
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * This will determine the intersection type of two list
     * Note: undirected 1 vs 2
     * @param list1
     * @param list2
     * Type 1: total 1,2,3 vs 1,2,3
     * Type 2: partial 1,2,3 vs 1,2,3,4,5 ->
     * Type 5: partial  1,2,3,4,5 vs 1,2,3 <-
     * Type 3: partial 1,2,3 vs 2,3,4
     * Type 4: disjoint 1,2,3 vs 4,5,6
     */
    public static <T> int intersection_type(List<T> list1, List<T> list2) {
        List<T> tmp = intersection(list1, list2);
        if (tmp.size() == list2.size() && tmp.size() == list1.size())
            return 1;
        if (tmp.size() < list2.size() && tmp.size() == list1.size())
            return 2;
        if (tmp.size() < list1.size() && tmp.size() == list2.size())
            return 5;
        if (tmp.size() > 0)
            return 3;
        return 4;
    }

    public static <T> List<T> intersection(List<T> list1, List<T> list2) {
        List<T> list = new ArrayList<T>();

        for (T t : list1) {
            if (list2.contains(t)) {
                list.add(t);
            }
        }

        return list;
    }
}

Related

  1. Intersection(List a, List b)
  2. intersection(List list1, List list2)
  3. intersection(List arrayOne, List arrayTwo)
  4. intersection(List listOne, List listTwo)
  5. intersection(List set1, List set2)
  6. intersectionInplace(List> sets)
  7. intersectList(List list1, List list2)
  8. intersectSortedAscending(List l1, List l2)
  9. isListIntersect(List list1, List list2)