ByteArrayRange implements Comparable<ByteArrayRange> { public static final Collection<ByteArrayRange> mergeIntersections( final Collection<ByteArrayRange> ranges, final MergeOperation op) { final List<ByteArrayRange> rangeList = new ArrayList<>(ranges); Collections.<ByteArrayRange>sort(rangeList); final List<ByteArrayRange> result = new ArrayList<>(); for (int i = 0; i < rangeList.size();) { ByteArrayRange r1 = rangeList.get(i); int j = i + 1; for (; j < rangeList.size(); j++) { final ByteArrayRange r2 = rangeList.get(j); if (r1.intersects(r2)) { if (op.equals(MergeOperation.UNION)) { r1 = r1.union(r2); } else { r1 = r1.intersection(r2); } } else { break; } } i = j; result.add(r1); } return result; } ByteArrayRange(final byte[] start, final byte[] end);  ByteArrayRange(final byte[] start, final byte[] end, final boolean singleValue); byte[] getStart(); byte[] getEnd(); byte[] getEndAsNextPrefix(); boolean isSingleValue(); @Override int hashCode(); @Override boolean equals(final Object obj); boolean intersects(final ByteArrayRange other); ByteArrayRange intersection(final ByteArrayRange other); ByteArrayRange union(final ByteArrayRange other); @Override int compareTo(final ByteArrayRange other); static final Collection<ByteArrayRange> mergeIntersections(
      final Collection<ByteArrayRange> ranges,
      final MergeOperation op);  }
@Test public void bigTest() { final List<ByteArrayRange> l2 = new ArrayList<>(); for (int i = 0; i < 3000; i++) { String seed = UUID.randomUUID().toString(); for (int j = 0; j < 500; j++) { l2.add( new ByteArrayRange( new ByteArray(seed).getBytes(), new ByteArray(increment(seed)).getBytes())); seed = increment(seed); } } ByteArrayRange.mergeIntersections(l2, MergeOperation.INTERSECTION); }