Java Iterator mergeIterator( final Iterator iterator1, final Iterator iterator2)

Here you can find the source of mergeIterator( final Iterator iterator1, final Iterator iterator2)

Description

merge Iterator

License

Apache License

Declaration

public static <T> Iterator<T> mergeIterator(
        final Iterator<T> iterator1, final Iterator<T> iterator2) 

Method Source Code

//package com.java2s;
/*//ww w .  j  a  va 2 s .  c o m
 * Copyright 2008-2009 the original ???(zyc@hasor.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.Iterator;

public class Main {

    public static <T> Iterator<T> mergeIterator(
            final Iterator<T> iterator1, final Iterator<T> iterator2) {
        final Iterator<T> i1 = iterator1 != null ? iterator1
                : new ArrayList<T>(0).iterator();
        final Iterator<T> i2 = iterator2 != null ? iterator2
                : new ArrayList<T>(0).iterator();
        return new Iterator<T>() {
            private Iterator<T> it = i1;

            @Override
            public boolean hasNext() {
                return i1.hasNext() || i2.hasNext() ? true : false;
            }

            @Override
            public T next() {
                if (this.it.hasNext() == false) {
                    this.it = i2;
                }
                return this.it.next();
            }

            @Override
            public void remove() {
                this.it.remove();
            }
        };
    }
}

Related

  1. length(Iterator iter)
  2. limit(final Iterator iterator, final int limit)
  3. listOfIterator(Iterator iterator)
  4. makeCollection(final Iterator i)
  5. max(final Iterator iter, final int max)
  6. moveNext(Iterator iterator)
  7. multiIterator(Iterator... iterable)
  8. multiply(String string, Iterator placeholders, Iterator replacements)
  9. multiply(String string, Iterator placeholders, Iterator replacements)