Here you can find the source of mergeIterator( final Iterator
public static <T> Iterator<T> mergeIterator( final Iterator<T> iterator1, final Iterator<T> iterator2)
//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(); } }; } }