Split Collection to two list depend on criteria Let's say I have a List contains numbers: 

1,2,3,4,5,6,7,8,9

and I would like to split this to two lists:


odd numbers
even numbers


So I can do this two ways: 

Solution 1:

Create two methods:

List&lt;Integer&gt; filterOdd(List&lt;Integer&gt; numbers){
    List&lt;Integer&gt; result  = new ArrayList&lt;Integer&gt;();
    for(Integer n : numbers){
        if(n % 2 != 0){
            result.add(n);
        }
    }
    return result;
}

List&lt;Integer&gt; filterEven(List&lt;Integer&gt; numbers){
    List&lt;Integer&gt; result  = new ArrayList&lt;Integer&gt;();
    for(Integer n : numbers){
        if(n % 2 == 0){
            result.add(n);
        }
    }
    return result;
 }


and in the code I will call: 

List&lt;Integer&gt; numbers = Arrays.asList(new Integer[] { 1,2,3,4,5,6,7,8,9 });

List&lt;Integer&gt; oddNumbers = filterOdd(numbers);
List&lt;Integer&gt; evenNumbers = filterEven(numbers);


and do something with these lists.

Disadvantage: 

I do two loops over one collection.

Solution 2: 

Create one method:

public void filter(List&lt;Integer&gt; numbers, List&lt;Integer&gt; oddNumbers, List&lt;Integer&gt; evenNumbers){
    for(Integer n : numbers){
        if(n % 2 != 0){
            oddNumbers.add(n);
        }else{
            evenNumbers.add(n);
        }
    }
}


and in the code I will call: 

List&lt;Integer&gt; numbers = Arrays.asList(new Integer[] { 1,2,3,4,5,6,7,8,9 });
List&lt;Integer&gt; oddNumbers  = new ArrayList&lt;Integer&gt;();
List&lt;Integer&gt; evenNumbers = new ArrayList&lt;Integer&gt;();
filter(numbers, oddNumbers, evenNumbers);


Disadvantage:

I hear that assigning results for parameters is bad practice. 

Which solution is better?
Here is a solution which replicates Guava's Predicate interface. Since this interface is really easy, here is how it is done:

public interface Predicate&lt;T&gt;
{
    // returns true if input obeys the predicate
    boolean apply(T input);
}


Given this interface, implement it for your types; and then do a method like this:

public static &lt;T&gt; List&lt;List&lt;T&gt;&gt; filteredLists(final List&lt;T&gt; list,
    final List&lt;Predicate&lt;T&gt;&gt; predicates)
{
    final int size = predicates.size();
    final List&lt;List&lt;T&gt;&gt; ret = new ArrayList&lt;List&lt;T&gt;&gt;(size);

    // Fill ret with initial lists
    for (int i = 0; i &lt; size; i++)
        ret.add(new ArrayList&lt;T&gt;());

    // Now walk the predicates and add to the necessary lists

    for (final T element: list)
        for (int i = 0; i &lt; size; i++)
            if (predicates.get(i).apply(element))
                ret.get(i).add(element);

    // Done! Return...
    return ret;
}


Now, if you had a utility class, say MyPredicates, with two methods even() and odd(), you could write:

final List&lt;List&lt;Integer&gt;&gt; filtered = filteredList(inputList,
    Arrays.asList(MyPredicates.even(), MyPredicates.odd()));


The "drawback" here is that it is up to the caller to remind what predicates where in what order in the calling lists, of course.

As to your MyPredicates hypothetical class:

public static final class MyPredicates
{
    // No instansiation
    private MyPredicates()
    {
    }

    public static Predicate&lt;Integer&gt; even()
    {
        return new Predicate&lt;Integer&gt;()
        {
            @Override
            public boolean apply(final Integer input)
            {
                return input.intValue() % 2 == 0;
            }
        }
    }

    public static Predicate&lt;Integer&gt; odd()
    {
        return new Predicate&lt;Integer&gt;()
        {
            @Override
            public boolean apply(final Integer input)
            {
                return input.intValue() % 2 == 1;
            }
        }
    }
}