@Override public <T> T[] toArray(T[] a) { Object[] kvs = raw_array(); // Estimate size of array; be prepared to see more or fewer elements int sz = size(); T[] r = a.length >= sz ? a : (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), sz); // Fast efficient element walk. int j=0; for( int i=0; i<len(kvs); i++ ) { Object K = key(kvs,i); Object V = Prime.unbox(val(kvs,i)); if( K != null && K != TOMBSTONE && V != null && V != TOMBSTONE ) { if( j >= r.length ) { int sz2 = (int)Math.min(Integer.MAX_VALUE-8,((long)j)<<1); if( sz2<=r.length ) throw new OutOfMemoryError("Required array size too large"); r = Arrays.copyOf(r,sz2); } r[j++] = (T)K; } } if( j <= a.length ) { // Fit in the original array? if( a!=r ) System.arraycopy(r,0,a,0,j); if( j<a.length ) r[j++]=null; // One final null not in the spec but in the default impl return a; // Return the original } return Arrays.copyOf(r,j); } };
@Override public <T> T[] toArray(T[] a) { Object[] kvs = raw_array(); // Estimate size of array; be prepared to see more or fewer elements int sz = size(); T[] r = a.length >= sz ? a : (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), sz); // Fast efficient element walk. int j=0; for( int i=0; i<len(kvs); i++ ) { Object K = key(kvs,i); Object V = Prime.unbox(val(kvs,i)); if( K != null && K != TOMBSTONE && V != null && V != TOMBSTONE ) { if( j >= r.length ) { int sz2 = (int)Math.min(Integer.MAX_VALUE-8,((long)j)<<1); if( sz2<=r.length ) throw new OutOfMemoryError("Required array size too large"); r = Arrays.copyOf(r,sz2); } r[j++] = (T)K; } } if( j <= a.length ) { // Fit in the original array? if( a!=r ) System.arraycopy(r,0,a,0,j); if( j<a.length ) r[j++]=null; // One final null not in the spec but in the default impl return a; // Return the original } return Arrays.copyOf(r,j); } };