Here you can find the source of merge(List
public static <K extends Comparable<K>> List<K> merge(List<K> list1, List<K> list2)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.List; public class Main { /**/*from w ww. ja v a2 s. c o m*/ * Merges two sorted segments into a single sorted list. */ public static <K extends Comparable<K>> List<K> merge(List<K> list1, List<K> list2) { List<K> newList = new ArrayList<>(list1.size() + list2.size()); int offset1 = 0; int offset2 = 0; while (!list1.isEmpty() && !list2.isEmpty()) { K item1 = list1.get(offset1); K item2 = list2.get(offset2); if (item1.compareTo(item2) <= 0) { newList.add(item1); list1.remove(offset1); } else { newList.add(item2); list2.remove(offset2); } if (offset1 == list1.size()) { newList.addAll(list2); } if (offset2 == list2.size()) { newList.addAll(list1); } } return newList; } }