private void initializeCellSet(int numOfCells, MemStoreSegmentsIterator iterator,
    MemStoreCompactionStrategy.Action action) {
    boolean merge = (action == MemStoreCompactionStrategy.Action.MERGE
      || action == MemStoreCompactionStrategy.Action.MERGE_COUNT_UNIQUE_KEYS);
    ExtendedCell[] cells = new ExtendedCell[numOfCells]; // build the Cell Array
    int i = 0;
    int numUniqueKeys = 0;
    Cell prev = null;
    while (iterator.hasNext()) {
      ExtendedCell c = iterator.next();
      // The scanner behind the iterator is doing all the elimination logic
      if (merge) {
        // if this is merge we just move the Cell object without copying MSLAB
        // the sizes still need to be updated in the new segment
        cells[i] = c;
      } else {
        // now we just copy it to the new segment (also MSLAB copy)
        cells[i] = maybeCloneWithAllocator(c, false);
      }
      // second parameter true, because in compaction/merge the addition of the cell to new segment
      // is always successful
      updateMetaInfo(cells[i], true, null); // updates the size per cell
      if (action == MemStoreCompactionStrategy.Action.MERGE_COUNT_UNIQUE_KEYS) {
        // counting number of unique keys
        if (prev != null) {
          if (!CellUtil.matchingRowColumnBytes(prev, c)) {
            numUniqueKeys++;
          }
        } else {
          numUniqueKeys++;
        }
      }
      prev = c;
      i++;
    }
    if (action == MemStoreCompactionStrategy.Action.COMPACT) {
      numUniqueKeys = numOfCells;
    } else if (action != MemStoreCompactionStrategy.Action.MERGE_COUNT_UNIQUE_KEYS) {
      numUniqueKeys = CellSet.UNKNOWN_NUM_UNIQUES;
    }
    // build the immutable CellSet
    CellArrayMap<ExtendedCell> cam = new CellArrayMap<>(getComparator(), cells, 0, i, false);
    this.setCellSet(null, new CellSet<>(cam, numUniqueKeys)); // update the CellSet of this Segment
  }
------------------------------------------------------------------------ C-tor to be used when
new CellChunkImmutableSegment is built as a result of flattening of CSLMImmutableSegment The
given iterator returns the Cells that "survived" the compaction.
hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/CellArrayImmutableSegment.java