0
public class CollectionsFilter {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7,
                8, 9, 10 });
        Collection<Integer> evenNumbers = Utils.filter(list,
                new Predicate<Integer>() {
                    public boolean apply(Integer i) {
                        if (i % 2 == 0) {
                            return true;
                        }
                        return false;
                    }
                });

        Collection<Integer> oddNumbers = Utils.filter(list,
                new Predicate<Integer>() {
                    public boolean apply(Integer i) {
                        if (i % 2 != 0) {
                            return true;
                        }
                        return false;
                    }
                });
        System.out.println("EVEN Numbers > " + evenNumbers);
        System.out.println("ODD Numbers > " + oddNumbers);
    }

}

where my Utils.filter() method is :

public static <T> Collection<T> filter(Collection<T> target,
            Predicate<T> predicate) {
        Collection<T> filteredCollection = new ArrayList<T>();
        for (T t : filteredCollection) {
            if (predicate.apply(t)) {
                filteredCollection.add(t);
            }
        }
        return filteredCollection;
    }

and the Prdicate:

public interface Predicate<T> {
    public boolean apply(T type);
}
jai
  • 21,519
  • 31
  • 89
  • 120
  • What results do you get? – Mike Valenty Nov 16 '09 at 00:30
  • Also, you are using a bad check for oddness: http://findbugs.sourceforge.net/bugDescriptions.html#IM_BAD_CHECK_FOR_ODD – Adam Goode Nov 16 '09 at 00:31
  • No, he's not. He's actually using %2 != 0 as suggested in your link – Jorn Nov 16 '09 at 00:32
  • However, you can just return the value of the mod check, instead of placing it in an if statement – Jorn Nov 16 '09 at 00:33
  • its printing : EVEN Numbers > [] ODD Numbers > [] I was trying this(http://stackoverflow.com/questions/122105/java-what-is-the-best-way-to-filter-a-collection) example. – jai Nov 16 '09 at 00:37

2 Answers2

5

First of all, don't write this kind of code yourself. There's Google Collections for that.

Having said that: Try to iterate over target instead of over filteredCollection in your filter() method, that should fix it.

Jorn
  • 20,612
  • 18
  • 79
  • 126
  • Thanks Jorn. It resolved. I've overseen that, as Eclipse has generated my 'for' loop. BTW, do you think it is worth to add a jar just for using one of its method. Just asking your opinion on this :) Cheers. – jai Nov 16 '09 at 00:40
  • For just this one method, probably not. But since it's open source, you can always rip that one method out of Google Collections and copy it to your source. The plus on including the jar is, you get access to all classes and methods in it, which can probably use to do other things to collections. If this happens a lot, the dependency makes your code a lot more consistent, having just library calls to operate on collections instead of writing your own method for everything. – Jorn Nov 16 '09 at 00:53
4

That's because your Util.filter runs over empty filteredCollection which it creates in the beginning.

Dmitry
  • 3,740
  • 15
  • 17