简体   繁体   中英

How to compare two ordered lists

ArrayList thisDataList and dataList are two ordered lists which contain a lot of data. "different"is an array of ArrayList which will contain in the first row data that we have in thisDataList but not in dataList and in the second row will contain data which we have in dataList but not in thisDataList, i use this method which works with small data but not more than files of 50Mo, could you give a solution i make the same algorithm using the fact that i have "ordered" lists to be faster?

I'm using Java 1.6

ArrayList<?>[] different = new ArrayList<?>[2];
ArrayList<String> tmp= new ArrayList<String>(thisDataList);

thisDataList.removeAll( dataList );
different[0]=new ArrayList<String>(thisDataList);

thisDataList= tmp;
dataList.removeAll( thisDataList );

different[1]=new ArrayList<String>(dataList);

Starting with the first elements of the lists. For the elements at the current index in each list, if one element is less than the other then it is unique to that list (since the lists are sorted and the other list could never find this lesser element farther on) so increment that lists current index and add that element as an "extra" of that list, repeat with the new current indexes.

See below:

public static void main(String[] args) {
    List<Integer> list1 = Arrays.asList(1, 2, 4, 6, 7, 8);
    List<Integer> list2 = Arrays.asList(1, 3, 4, 5, 6, 9);

    List<List<Integer>> listExtras = getExtraElementsOfLists(list1, list2);
    List<Integer> list1Extras = listExtras.get(0);
    List<Integer> list2Extras = listExtras.get(1);

    System.out.println("List 1 extras:");
    for (Integer i : list1Extras)
        System.out.println(i);

    System.out.println();
    System.out.println("List 2 extras:");
    for (Integer i : list2Extras)
        System.out.println(i);
}


public static <T extends Comparable<T>> List<List<T>> getExtraElementsOfLists(List<T> list1,
        List<T> list2) {
    Collections.sort(list1);
    Collections.sort(list2);

    List<T> list1Extras = new ArrayList<>();
    List<T> list2Extras = new ArrayList<>();
    int index1 = 0;
    int index2 = 0;
    while (index1 < list1.size() && index2 < list2.size()) {
        T value1 = list1.get(index1);
        T value2 = list2.get(index2);
        int delta = value1.compareTo(value2);
        if (delta < 0) { // val1 < val2, list2 !contain val1
            list1Extras.add(value1);
            index1++;
        } else if (delta > 0) { // val1 > val2, list1 !contain val2
            list2Extras.add(value2);
            index2++;
        } else { // both have the same value
            index1++;
            index2++;
        }
    }
    // whichever list still has elements, add them all to its extras
    for (; index1 < list1.size(); index1++)
        list1Extras.add(list1.get(index1));
    for (; index2 < list2.size(); index2++)
        list2Extras.add(list2.get(index2));

    return Arrays.asList(list1Extras, list2Extras);
}

Aside from the sorting, the time complexity for this is linear .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM