docs: @SuppressWarnings("null")
    public byte[] toByteArray(final ArrayOfItemsSerDe<T> serDe) {
      final int preLongs;
      final int outBytes;
      final boolean empty = isEmpty();
      final int activeItems = getNumActiveItems();
      byte[] bytes = null;
      if (empty) {
        preLongs = 1;
        outBytes = 8;
      } else {
        preLongs = Family.FREQUENCY.getMaxPreLongs();
        bytes = serDe.serializeToByteArray(hashMap.getActiveKeys());
        outBytes = ((preLongs + activeItems) << 3) + bytes.length;
      }
      final byte[] outArr = new byte[outBytes];
      final WritableMemory mem = WritableMemory.wrap(outArr);
  
      // build first preLong empty or not
      long pre0 = 0L;
      pre0 = insertPreLongs(preLongs, pre0);                  //Byte 0
      pre0 = insertSerVer(SER_VER, pre0);                     //Byte 1
      pre0 = insertFamilyID(Family.FREQUENCY.getID(), pre0);  //Byte 2
      pre0 = insertLgMaxMapSize(lgMaxMapSize, pre0);          //Byte 3
      pre0 = insertLgCurMapSize(hashMap.getLgLength(), pre0); //Byte 4
      pre0 = empty ? insertFlags(EMPTY_FLAG_MASK, pre0) : insertFlags(0, pre0); //Byte 5
  
      if (empty) {
        mem.putLong(0, pre0);
      } else {
        final long pre = 0;
        final long[] preArr = new long[preLongs];
        preArr[0] = pre0;
        preArr[1] = insertActiveItems(activeItems, pre);
        preArr[2] = this.streamWeight;
        preArr[3] = this.offset;
        mem.putLongArray(0, preArr, 0, preLongs);
        final int preBytes = preLongs << 3;
        mem.putLongArray(preBytes, hashMap.getActiveValues(), 0, activeItems);
        mem.putByteArray(preBytes + (this.getNumActiveItems() << 3), bytes, 0, bytes.length);
      }
      return outArr;
    }