# Class MergingIterator<T>

java.lang.Object
htsjdk.samtools.util.MergingIterator<T>

All Implemented Interfaces:
  `CloseableIterator<T>, Closeable, AutoCloseable, Iterator<T>`

public class MergingIterator<T> extends Object implements CloseableIterator<T>
An iterator over Iterators that return Ts. Calling next() returns the next T ordered according to Comparator provided at construction time. Importantly, the elements in the input Iterators must already be sorted according to the provided Comparator.

## Constructor Summary

Constructors
Constructor
Description
`MergingIterator(Comparator<T> comparator, Collection<CloseableIterator<T>> iterators)`
Creates a MergingIterator over the given Collection of iterators whose elements will be returned in the order defined by the given Comparator.

## Method Summary

Modifier and Type
Method
Description
`void`
`close()`
Closes every CloseableIterator in this MergingIterator.
`boolean`
`hasNext()`
`T`
`next()`
`void`
`remove()`
Unsupported.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface CloseableIterator

`stream, toList`

### Methods inherited from interface Iterator

`forEachRemaining`

## Constructor Details

### MergingIterator

public MergingIterator(Comparator<T> comparator, Collection<CloseableIterator<T>> iterators)
Creates a MergingIterator over the given Collection of iterators whose elements will be returned in the order defined by the given Comparator.

## Method Details

### hasNext

public boolean hasNext()

Specified by:
  `hasNext` in interface `Iterator<T>`

### next

public T next()

Specified by:
  `next` in interface `Iterator<T>`

### remove

public void remove()
Unsupported.

Specified by:
  `remove` in interface `Iterator<T>`

### close

public void close()
Closes every CloseableIterator in this MergingIterator. After calling, calls to hasNext() will always return false.

Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Closeable`
Specified by:
  `close` in interface `CloseableIterator<T>`