@ SuppressWarnings ( "unchecked" ) public T [ ] pollToArray ( T [ ] array ) { int currentIndex = 0 ; LinkedElement < T > current = null ; if ( array . length == 0 ) { return array ; } if ( array . length > size ) { array = ( T [ ] ) java . lang . reflect . Array . newInstance ( array . getClass ( ) . getComponentType ( ) , size ) ; } // do fast polling if the entire set needs to be fetched if ( array . length == size ) { for ( int i = 0 ; i < entries . length ; i ++ ) { current = entries [ i ] ; while ( current != null ) { array [ currentIndex ++ ] = current . element ; current = current . next ; } } this . clear ( ) ; return array ; } boolean done = false ; int currentBucketIndex = 0 ; while ( ! done ) { current = entries [ currentBucketIndex ] ; while ( current != null ) { array [ currentIndex ++ ] = current . element ; current = current . next ; entries [ currentBucketIndex ] = current ; size -- ; modification ++ ; if ( currentIndex == array . length ) { done = true ; break ; } } currentBucketIndex ++ ; } shrinkIfNecessary ( ) ; return array ; }
Get array . length elements from the set and put them into the array .