Here you can find the source of mergeSortedLists(List
Parameter | Description |
---|---|
dest | a parameter |
src | a parameter |
@SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> void mergeSortedLists(List<T> dest, List<T>[] src, boolean removeDuplicates)
//package com.java2s; /*/*from w ww. j a v a 2 s.c o m*/ * ***** BEGIN LICENSE BLOCK ***** * Zimbra Collaboration Suite Server * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Synacor, Inc. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software Foundation, * version 2 of the License. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with this program. * If not, see <https://www.gnu.org/licenses/>. * ***** END LICENSE BLOCK ***** */ import java.util.Iterator; import java.util.List; public class Main { /** * Merge two sorted Lists * * I could have used the Java Set collection here...unfortunately the only ordered set * is their TreeSet, and set union/subtraction therefore take NlogN with a pretty big * constant overhead. * * @param dest * @param src */ @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> void mergeSortedLists(List<T> dest, List<T>[] src, boolean removeDuplicates) { int numSrc = 0; for (int i = 0; i < src.length; i++) { if (src[i] != null) { numSrc++; } } if (numSrc == 1) { for (int i = 0; i < src.length; i++) { if (src[i] != null) { dest.addAll(src[i]); return; } } } Iterator<T> iter[] = new Iterator[numSrc]; int iterOffset = 0; for (int i = 0; i < src.length; i++) { if (src[i] != null) { iter[iterOffset++] = src[i].iterator(); } } int numItersActive = src.length; // holds the next values of each iterator T nextValue[] = (T[]) new Comparable[src.length]; T lowestValue = null; int lowestValueOffset = -1; T lastAdded = null; // prime the pump for (int i = 0; i < iter.length; i++) { if (iter[i].hasNext()) { nextValue[i] = iter[i].next(); if (lowestValue == null || (lowestValue.compareTo(nextValue[i]) > 0)) { lowestValue = nextValue[i]; lowestValueOffset = i; } } else { iter[i] = null; numItersActive--; } } while (numItersActive > 0) { // grab lowest value from the src list, put it on the return list if ((!removeDuplicates) || (lastAdded == null)) { dest.add(nextValue[lowestValueOffset]); lastAdded = nextValue[lowestValueOffset]; nextValue[lowestValueOffset] = null; } else { if (!lastAdded.equals(nextValue[lowestValueOffset])) { dest.add(nextValue[lowestValueOffset]); lastAdded = nextValue[lowestValueOffset]; } nextValue[lowestValueOffset] = null; } // iterate the proper src list if (iter[lowestValueOffset].hasNext()) { nextValue[lowestValueOffset] = iter[lowestValueOffset].next(); } else { iter[lowestValueOffset] = null; numItersActive--; } // find the next-lowest-value lowestValue = null; lowestValueOffset = -1; for (int i = 0; i < src.length; i++) { if (lowestValue == null || ((nextValue[i] != null) && (lowestValue.compareTo(nextValue[i]) > 0))) { lowestValue = nextValue[i]; lowestValueOffset = i; } } } } }