Jonathon K
Jonathon K

Reputation: 19

Merging two sorted Arraylists into one sorted Arraylist

My code should merge two already sorted arraylists into one sorted arraylist and if one of the arraylists used is not sorted then it should return null.

public class MergeSorted {
public static void merge(ArrayList<Integer> a, ArrayList<Integer> b) {

    for (int i = 0, j = 0; j < b.size(); i++) {
        if (i == a.size() || a.get(i) > a.get(j)) {
            a.add(i, b.get(j++));
        }
    }
}  
}

This is what I attempted but can't get the idea of returning null if they are not equal, Im new to java and this is my second week so please be patient with me. I know I should have an if statement checking if they are sorted and an else but what should I include inside the if?

Upvotes: 1

Views: 3582

Answers (3)

Karthik R
Karthik R

Reputation: 5786

  • Please refer to this link for comparing if the List implementation is equal. Please note that the ArrayList implementation of yours contains List<Integer> and thus the sorting takes place in the natural order. In case of equal return true or else false. You can change it as required to return any flag.

Sort and compare 2 lists if equal - List of String

  • There are useful Apache Common library classes available, which can help you in merging the array lists that are sorted : This will give you what you are looking for. org.apache.commons.collections4 package. Apache Common Lib version 4 - collate()

eg., List<Integer> mergedList = CollectionUtils.collate(list1, list2);

Upvotes: 1

Karan Sharma
Karan Sharma

Reputation: 2599

Problem

Check if the two lists are sorted, if they are then it will merge the two lists into a single sorted list, whereas if the lists are not sorted return null.

Code Solution:

Try the following code:

public class MergeSorted {
public static List merge(List<Integer> aList, List<Integer> bList) {

    List mergeList = new ArrayList<Integer>();

    //checking if list 'A' is sorted
    List temp = new ArrayList(aList);
    Collections.sort(temp);
    boolean aSorted = temp.equals(aList);

    //checking if list 'B' is sorted
    temp = new ArrayList(bList);
    Collections.sort(temp);
    boolean bSorted = temp.equals(bList);

    //if both lists are sorted then merge them
    if(true == aSorted && true == bSorted) {
        mergeList.addAll(aList);
        mergeList.addAll(bList);
        Collections.sort(mergeList);
    }

   return mergeList; 
    }
  }

Upvotes: 1

FlasH from Ru
FlasH from Ru

Reputation: 1215

The generic way is something like this:

public class MergeSort {

    public static <T extends Comparable<? super T>> List<T> merge(List<T> a, List<T> b) {
        if (!isSorted(a) || !isSorted(b)) {
            return null;
        }

        final List<T> result = new ArrayList<T>(a.size() + b.size());
        result.addAll(a);
        result.addAll(b);

        Collections.sort(result);

        return result;
    }

    private static <T extends Comparable<? super T>> boolean isSorted(final List<T> list) {
        T prevItem = null;
        for (T item : list) {

            if (prevItem != null && item.compareTo(prevItem) < 0) {
                return false;
            }

            prevItem = item;
        }

        return true;
    }

}

You can easily replace all these generic Ts with Integer if you only need it for them...

Upvotes: 0

Related Questions