集册 Java实例教程 合并两个同构pre

合并两个同构pre

欢马劈雪     最近更新时间:2020-01-02 10:19:05

490
合并两个同构的预排序可iterable输入。

/*

 * The MIT License

 *

 * Copyright 2013 RBC1B.

 *

 * Permission is hereby granted, free of charge, to any person obtaining a copy

 * of this software and associated documentation files (the "Software"), to deal

 * in the Software without restriction, including without limitation the rights

 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

 * copies of the Software, and to permit persons to whom the Software is

 * furnished to do so, subject to the following conditions:

 *

 * The above copyright notice and this permission notice shall be included in

 * all copies or substantial portions of the Software.

 *

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

 * THE SOFTWARE.

 */

import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;/**来 自 时 代 J a v a 公 众 号**/

import java.util.Comparator;

import java.util.List;


public class Main{

    /**

     * Merge two homogeneous pre-sorted iterable inputs.

     * @param <T> Input type

     * @param left Left input

     * @param right Right input

     * @param comparator Comparator to compare inputs

     * @param callback Callback for each merge output

     */

    public static <T> void merge(Iterable<T> left, Iterable<T> right,

            final Comparator<T> comparator, Callback<T, T> callback) {

        merge(left, right, new Compare<T, T>() {

            @Override

            public int compare(T leftValue, T rightValue) {

                return comparator.compare(leftValue, rightValue);

            }

        }, callback);

    }

    /**

     * Merge two heterogenous pre-sorted iterable inputs.

     * @param <U> Left input type

     * @param <V> Right input type

     * @param left Left input

     * @param right Right input

     * @param comparator Comparator to compare inputs

     * @param callback Callback for each merge output

     *///来 自 时 代 J a v a - nowjava.com

    public static <U, V> void merge(Iterable<U> left, Iterable<V> right,

            Compare<U, V> comparator, Callback<U, V> callback) {

        Cursor<U> cursor1 = new IteratorCursor<U>(left.iterator());

        Cursor<V> cursor2 = new IteratorCursor<V>(right.iterator());


        while (cursor1.hasValue() && cursor2.hasValue()) {

            U value1 = cursor1.value();

            V value2 = cursor2.value();

            int n = comparator.compare(value1, value2);


            if (n == 0) {

                callback.output(value1, value2);

                cursor1.advance();

         
展开阅读全文