@Override
    public T next() {
      T result;
      // use at most 2 comparisons to get the next item
      if (lessThan(ak, bk, a, b)) {
        if (lessThan(ak, ck, a, c)) {
          // ACB / ABC
          result = a;
          if (inputA.hasNext()) {
            a = inputA.next();
            ak = a.key();
          } else {
            a = null;
            ak = Long.MAX_VALUE;
          }
        } else {
          // CBA
          result = c;
          if (inputC.hasNext()) {
            c = inputC.next();
            ck = c.key();
          } else {
            c = null;
            ck = Long.MAX_VALUE;
          }
        }
      } else if (lessThan(ck, bk, c, b)) {
        // CAB
        result = c;
        if (inputC.hasNext()) {
          c = inputC.next();
          ck = c.key();
        } else {
          c = null;
          ck = Long.MAX_VALUE;
        }
      } else if (bk == Long.MAX_VALUE) {
        throw new NoSuchElementException();
      } else {
        // BAC / BCA
        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