Common elements in two lists

后端 未结 14 804
无人共我
无人共我 2020-11-22 12:22

I have two ArrayList objects with three integers each. I want to find a way to return the common elements of the two lists. Has anybody an idea how I can achiev

相关标签:
14条回答
  • 2020-11-22 12:53

    Some of the answers above are similar but not the same so posting it as a new answer.

    Solution:
    1. Use HashSet to hold elements which need to be removed
    2. Add all elements of list1 to HashSet
    3. iterate list2 and remove elements from a HashSet which are present in list2 ==> which are present in both list1 and list2
    4. Now iterate over HashSet and remove elements from list1(since we have added all elements of list1 to set), finally, list1 has all common elements
    Note: We can add all elements of list2 and in a 3rd iteration, we should remove elements from list2.

    Time complexity: O(n)
    Space Complexity: O(n)

    Code:

    import com.sun.tools.javac.util.Assert;
    import org.apache.commons.collections4.CollectionUtils;
    
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        list1.add(5);
    
        List<Integer> list2 = new ArrayList<>();
        list2.add(1);
        list2.add(3);
        list2.add(5);
        list2.add(7);
        Set<Integer> toBeRemoveFromList1 = new HashSet<>(list1);
        System.out.println("list1:" + list1);
        System.out.println("list2:" + list2);
        for (Integer n : list2) {
            if (toBeRemoveFromList1.contains(n)) {
                toBeRemoveFromList1.remove(n);
            }
        }
        System.out.println("toBeRemoveFromList1:" + toBeRemoveFromList1);
        for (Integer n : toBeRemoveFromList1) {
            list1.remove(n);
        }
        System.out.println("list1:" + list1);
        System.out.println("collectionUtils:" + CollectionUtils.intersection(list1, list2));
        Assert.check(CollectionUtils.intersection(list1, list2).containsAll(list1));
    

    output:

    list1:[1, 2, 3, 4, 5]
    list2:[1, 3, 5, 7]
    toBeRemoveFromList1:[2, 4]
    list1:[1, 3, 5]
    collectionUtils:[1, 3, 5]
    
    0 讨论(0)
  • 2020-11-22 12:54

                List<String> lista =new ArrayList<String>();
                List<String> listb =new ArrayList<String>();
    
                lista.add("Isabella");
                lista.add("Angelina");
                lista.add("Pille");
                lista.add("Hazem");
    
                listb.add("Isabella");
                listb.add("Angelina");
                listb.add("Bianca");
    
                // Create an aplusb list which will contain both list (list1 and list2) in which common element will occur twice 
                List<String> listapluslistb =new ArrayList<String>(lista);    
                listapluslistb.addAll(listb);
    
                // Create an aunionb set which will contain both list (list1 and list2) in which common element will occur once
                Set<String> listaunionlistb =new HashSet<String>(lista);
                listaunionlistb.addAll(listb);
    
                for(String s:listaunionlistb)
                {
                    listapluslistb.remove(s);
                }
                System.out.println(listapluslistb);
    
    0 讨论(0)
  • 2020-11-22 12:55

    Why reinvent the wheel? Use Commons Collections:

    CollectionUtils.intersection(java.util.Collection a, java.util.Collection b)
    
    0 讨论(0)
  • 2020-11-22 12:55
    public <T> List<T> getIntersectOfCollections(Collection<T> first, Collection<T> second) {
            return first.stream()
                    .filter(second::contains)
                    .collect(Collectors.toList());
        }
    
    0 讨论(0)
  • 2020-11-22 12:55
        // Create two collections:
        LinkedList<String> listA =  new LinkedList<String>();
        ArrayList<String> listB =  new ArrayList<String>();
    
        // Add some elements to listA:
        listA.add("A");
        listA.add("B");
        listA.add("C");
        listA.add("D");
    
        // Add some elements to listB:
        listB.add("A");
        listB.add("B");
        listB.add("C");
    
        // use 
    
        List<String> common = new ArrayList<String>(listA);
        // use common.retainAll
    
        common.retainAll(listB);
    
        System.out.println("The common collection is : " + common);
    
    0 讨论(0)
  • 2020-11-22 12:55

    Below code Remove common elements in the list

    List<String> result =  list1.stream().filter(item-> !list2.contains(item)).collect(Collectors.toList());
    

    Retrieve common elements

    List<String> result = list1.stream()
                    .distinct()
                    .filter(list::contains)
                    .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题