[ 
https://issues.apache.org/jira/browse/COLLECTIONS-674?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16938352#comment-16938352
 ] 

Chen commented on COLLECTIONS-674:
----------------------------------


{code:java}
public static <E> Collection<E> drain(Collection<E> input, int start, int 
count) {
                if (null == input) {
                        throw new IllegalArgumentException("The Collection of 
Input can't be null.");
                }
                if (start < 0) {
                        throw new IllegalArgumentException("The Start can't 
less than 0.");
                }
                if (count < 1) {
                        throw new IllegalArgumentException("The Count can't 
less than 1.");
                }
                if (input.size() < start + count) {
                        throw new IllegalArgumentException(
                                        "The sum of start and count cann't be 
greater than the size of collection.");
                }

                Collection<E> result = new ArrayList<E>(count);
                Iterator<E> iterator = input.iterator();
                while (count > 0) {
                        result.add(iterator.next());
                        iterator.remove();
                        count = count - 1;
                }
                return result;
        }
{code}


> Add Collections Drain Method
> ----------------------------
>
>                 Key: COLLECTIONS-674
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-674
>             Project: Commons Collections
>          Issue Type: Improvement
>          Components: Collection
>    Affects Versions: 4.1
>            Reporter: David Mollitor
>            Priority: Major
>
> Add a {{Collections.drain()}} method which removes the first N elements from 
> the collection and returns them.  This method would have the side-effect of 
> modifying the input collections (due to removal).
>  
> {code:java}
> // Some suggestions
> void Collections.drain(Collection<T> from, Collection<T> to, int count);
> Collection<t> Collections.drain(Collection<t> from, int count);{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to