/* * [Merge.java] * * Summary: Various ways of merging two SortedArrayLists, also deduping. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.2 2003-10-06 * 1.3 2007-07-29 IntelliJ inspector lint, preparze ANT script, */ package com.mindprod.sorted; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import static java.lang.System.*; /** * Various ways of merging two SortedArrayLists, also deduping. *

* All methods are static. * * @author Roedy Green, Canadian Mind Products * @version 1.3 2007-07-29 IntelliJ inspector lint, preparze ANT script, * @since 2003-08-23 */ public final class Merge { /** * True if you want the TEST harness code. */ private static final boolean DEBUGGING = false; /** * Remove any matching exceptions from an array list. * * @param generic type of the SortedArrayLists, e.g. String * @param primary primary SortedArrayList, will be sorted by the comparator. * @param unwanted List of exception objects which will be removed if they exist in the primary list. Will be * sorted by the comparator. * @param comparator Comparator for comparing two elements in the SortedArrayLists. * * @return primary list with any matching objects in the exception list removed, and sorted in order by the * comparator. */ public static SortedArrayList allBut( SortedArrayList primary, SortedArrayList unwanted, Comparator comparator ) { return genericCombiner( primary, false, true, unwanted, false, false, comparator, false ); } /** * Remove any duplicates. The first object encountered is kept. Subsequent ones are removed. * * @param generic type of the SortedArrayLists, e.g. String * @param withDups SortedArrayList with duplicates. It will be sorted by the comparator. * @param comparator Compator for comparing two elements in the SortedArrayLists to see if they are duplicates. * * @return list with any duplicates removed and sorted in order by the comparator. */ @SuppressWarnings( { "SameParameterValue", "WeakerAccess" } ) public static SortedArrayList deDupKeepingFirst( SortedArrayList withDups, Comparator comparator ) { assert ( comparator != null ) : "can't have a null comparator."; withDups.sort( comparator ); int size = withDups.size(); SortedArrayList result = new SortedArrayList<>( size ); result.setComparator( comparator ); if ( size <= 1 ) { if ( size == 1 ) { result.add( withDups.get( 0 ) ); } result.sorted = true; return result; } E prev = withDups.get( 0 ); result.add( prev ); for ( int i = 1; i < size; i++ ) { E o = withDups.get( i ); if ( comparator.compare( o, prev ) != 0 ) { result.add( o ); prev = o; } } result.sorted = true; return result; } /** * Remove any duplicates. The last duplicate object encountered is kept. previous duplicates are removed. * * * @param generic type of the SortedArrayLists, e.g. String * @param withDups SortedArrayList with duplicates. It will be sorted by the comparator. * @param comparator Comparator for comparing two elements in the SortedArrayLists to see if they are duplicates. * must not be null. * * @return list with any duplicates removed and sorted in order by the comparator. */ public static SortedArrayList deDupKeepingLast( SortedArrayList withDups, Comparator comparator ) { assert ( comparator != null ) : "can't have a null comparator."; withDups.sort( comparator ); int size = withDups.size(); SortedArrayList result = new SortedArrayList<>( size ); result.setComparator( comparator ); if ( size <= 1 ) { if ( size == 1 ) { result.add( withDups.get( 0 ) ); } result.sorted = true; return result; } E prev = withDups.get( 0 ); for ( int i = 1; i < size; i++ ) { E o = withDups.get( i ); if ( comparator.compare( o, prev ) != 0 ) { result.add( prev ); } prev = o; } result.add( prev ); result.sorted = true; return result; } /** * Combine two SortedArrayLists matching using the supplied comparator. You decide precisely what you want included * in the result. If there are duplicates in primary and secondary they match correspondingly with the excess of * eitther considered as non-matching. pri sec matched=m u m u=unmatched possibilities include