@SuppressWarnings("unchecked")
    @Override
    public <R> R[] toArray(R[] a) {
        int size = size();
        if (size <= a.length) {
            Iterator<T> iter = iterator();
            int i = 0;
            while (iter.hasNext()) {
                a[i++] = (R) iter.next();
            }
            while (i < a.length) {
                a[i++] = null;
            }
            return a;
        } else {
            return (R[]) toArray();
        }
    }
This is a hash set which can be snapshotted.
<br>
See {@link SnapshottableHashTable} for more details about the implementation.
<br>
This class requires external synchronization.  Null values are not supported.

@param <T>   The value type of the set.
server-common/src/main/java/org/apache/kafka/timeline/TimelineHashSet.java