public static < T > List < Set < T > > split ( Set < T > set , int maxSize ) { Assert . notNull ( set , "Missing set to be split!" ) ; Assert . isTrue ( maxSize > 0 , "Max size must be > 0!" ) ; if ( set . size ( ) < maxSize ) { return Collections . singletonList ( set ) ; } List < Set < T > > list = new ArrayList <> ( ) ; Iterator < T > iterator = set . iterator ( ) ; while ( iterator . hasNext ( ) ) { Set < T > newSet = new HashSet <> ( ) ; for ( int j = 0 ; j < maxSize && iterator . hasNext ( ) ; j ++ ) { T item = iterator . next ( ) ; newSet . add ( item ) ; } list . add ( newSet ) ; } return list ; }
Splits set in 1 .. N chunks of maxSize or less