@Override
    public T next() {
      T result;
      if (lessThan(ak, bk, a, b)) {
        result = a;
        if (inputA.hasNext()) {
          a = inputA.next();
          ak = a.key();
        } else {
          a = null;
          ak = Long.MAX_VALUE;
        }
      } else if (bk == Long.MAX_VALUE) {
        throw new NoSuchElementException();
      } else {
        result = b;
        if (inputB.hasNext()) {
          b = inputB.next();
          bk = b.key();
        } else {
          b = null;
          bk = Long.MAX_VALUE;
        }
      }
      return result;
    }
Merges sorted iterators into a combined iterator over all the items.
planetiler-core/src/main/java/com/onthegomap/planetiler/collection/LongMerger.java