Java List Difference extractDifference(List l1, List l2, List in1not2, List in2not1, List inBothFrom1, List inBothFrom2, Comparator c)

Here you can find the source of extractDifference(List l1, List l2, List in1not2, List in2not1, List inBothFrom1, List inBothFrom2, Comparator c)

Description

extract Difference

License

Apache License

Declaration

public static void extractDifference(List l1, List l2, List in1not2, List in2not1, List inBothFrom1,
            List inBothFrom2, Comparator c) 

Method Source Code

//package com.java2s;
/*//  w w w .j  a  va2 s  .c o  m
 * Copyright 2004 Senunkan Shinryuu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    public static void extractDifference(List l1, List l2, List in1not2, List in2not1, List inBothFrom1,
            List inBothFrom2, Comparator c) {
        int index1 = 0;
        int index2 = 0;
        Object o1 = null;
        Object o2 = null;
        String curr1 = null;
        String curr2 = null;
        String next1 = null;
        String next2 = null;
        boolean add1 = false;
        boolean add2 = false;
        int compare = 0;
        // 1st the 2 list must be sorted
        if (c == null) {
            Collections.sort(l1);
            Collections.sort(l2);
        } else {
            Collections.sort(l1, c);
            Collections.sort(l2, c);
        }

        // then we loop thru the list
        while (index1 < (l1.size() - 1) && index2 < (l2.size() - 1)) {
            o1 = l1.get(index1);
            o2 = l2.get(index2);
            if (o1 == null) {
                index1++;
                continue;
            }
            if (o2 == null) {
                index2++;
                continue;
            }
            curr1 = o1.toString();
            curr2 = o2.toString();
            compare = curr1.compareTo(curr2);
            if (compare == 0) {
                index1++;
                index2++;
                add1 = true;
                add2 = true;
            } else if (compare > 0) {
                // curr1 is bigger than curr2
                if (index2 + 1 < l2.size()) {
                    next2 = (String) l2.get(index2 + 1);
                    compare = curr2.compareTo(next2);
                    if (compare > 0) {
                        // check that if the next element of a2 is still
                        // less than the current element of a1 then we will 
                        // add the curr2 element
                        add2 = true;
                        add1 = false;
                    } else {
                        // if the next element of a2 is same as or bigger
                        // then current element of a1 we will add curr2
                        in2not1.add(curr2);
                        add2 = false;
                        add1 = false;
                    }
                }
                index2++;
            } else if (compare < 0) {
                if (index1 + 1 < l1.size()) {
                    // check that if the next element of a1 is still
                    // less than the current element then we will add the
                    // curr2 element
                    next1 = (String) l1.get(index1 + 1);
                    compare = next1.compareTo(curr2);
                    if (compare < 0) {
                        add1 = true;
                        add2 = false;
                    } else {
                        // if the next element of a1 is same as or bigger
                        // then current element of a2 we will add curr1
                        in1not2.add(curr1);
                        add2 = false;
                        add1 = false;
                    }
                }
                index1++;
            }
            if (add1 && add2) {
                inBothFrom1.add(o1);
                inBothFrom2.add(o2);
            } else {
                if (add1) {
                    in1not2.add(o1);
                }
                if (add2) {
                    in2not1.add(o2);
                }
            }
        }

        // we reach the end of list 2 but not the end of list 1
        // or list 2 does nto contain any element
        // add all the rest of the l1 to collection
        if (index2 == l2.size() - 1 || l2.size() == 0) {
            curr2 = null;
            curr1 = null;
            o1 = null;
            o2 = null;
            if (l2.size() > 0) {
                o2 = l2.get(l2.size() - 1);
                curr2 = o2.toString();
                for (int i = index1; i < l1.size(); i++) {
                    o1 = l1.get(i);
                    curr1 = o1.toString();
                    compare = curr1.compareTo(curr2);
                    if (compare == 0) {
                        inBothFrom1.add(o1);
                    } else {
                        in1not2.add(o1);
                    }
                }
                // check that last element is not inside the rest of the list
                boolean addLastElement = true;
                if (curr2 != null) {
                    for (int i = index1; i < l1.size(); i++) {
                        o1 = l1.get(i);
                        curr1 = o1.toString();
                        compare = curr1.compareTo(curr2);
                        if (compare == 0) {
                            addLastElement = false;
                            break;
                        }
                    }
                }
                if (addLastElement) {
                    in2not1.add(o2);
                } else {
                    inBothFrom2.add(o2);
                }
            } else {
                // since l2 size is zero we just add l1's element
                for (int i = index1; i < l1.size(); i++) {
                    o1 = l1.get(i);
                    in1not2.add(o1);
                }
            }
        }

        // we reach the end of list 1 but not the end of list 2
        // or list 1 does nto contain any element
        // add all the rest of the l2 to collection
        if ((index1 == l1.size() - 1 && index2 != l2.size() - 1) || l1.size() == 0) {
            curr2 = null;
            curr1 = null;
            o1 = null;
            o2 = null;
            if (l1.size() > 0) {
                o1 = l1.get(l1.size() - 1);
                curr1 = o1.toString();
                for (int i = index2; i < l2.size(); i++) {
                    o2 = l2.get(i);
                    curr2 = o2.toString();
                    compare = curr1.compareTo(curr2);
                    if (compare == 0) {
                        inBothFrom2.add(o2);
                    } else {
                        in2not1.add(o2);
                    }
                }
                // check that last element is not inside the rest of the list
                boolean addLastElement = true;
                if (curr1 != null) {
                    for (int i = index2; i < l2.size(); i++) {
                        o2 = l2.get(i);
                        curr2 = o2.toString();
                        compare = curr2.compareTo(curr1);
                        if (compare == 0) {
                            addLastElement = false;
                            break;
                        }
                    }
                }
                if (addLastElement) {
                    in1not2.add(o1);
                } else {
                    inBothFrom1.add(o1);
                }
            } else {
                for (int i = index2; i < l2.size(); i++) {
                    o2 = l2.get(i);
                    in2not1.add(o2);
                }
            }
        }
    }
}

Related

  1. differentCategory(String prop1, String prop2, List cat1, List cat2)
  2. diffFloats(List a, List b)
  3. diffList(List aList, List bList)
  4. diffSet(List a, List b)
  5. diffSimple(String name, Object expected, Object actual, List messages)
  6. formatDifferentFileNames(List results)
  7. getDiffentElement(List minList, List maxList)
  8. getDifference(List bigger, List smaller)
  9. getDiffInfo(List infoList)