Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-359

A ∩ B != B ∩ A when duplicates are present in a list

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.2
    • Fix Version/s: 4.0-alpha1, 4.0
    • Component/s: Collection
    • Labels:
      None

      Description

      When duplicates are present in a list, ListUtils.intersection doesn't behave as expected. The intersection of two lists should give the same result regardless of which list comes first. ListUtils.intersection(A,B) should equal ListUtils.intersection(B,A). This is not the case when the list contains duplicates.

      Right now:
      [a, b][a, a, b, b] = [a, a, b, b]
      and
      [a, a, b, b][a, b] = [a, b]

      Expected behavior:

      [a, a, b, b][a, b] = [a, b]
      [a, b][a, a, b, b] = [a, b]

      Code demonstrating the problem.

      List A = new ArrayList();
      List B = new ArrayList();
      A.add("a");
      A.add("b");
      B.add("a");
      B.add("a");
      B.add("b");
      B.add("b");
      System.out.println("List A: " + A);
      System.out.println("List B: " + B);
      System.out.println("A ∩ B = " + ListUtils.intersection(A,B));
      System.out.println("B ∩ A = " +ListUtils.intersection(B,A));

      output:

      List A: [a, b]
      List B: [a, a, b, b]
      A ∩ B = [a, a, b, b]
      B ∩ A = [a, b]

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mwshead Mark Shead
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: