java - Why can't I use a String array with a method taking an Iterable as a parameter? -
i'm trying write own "functional" little lib in java. if have function :
public static <t> list<t> filter(iterable<t> source,booleantest predicate) { list<t> results = new arraylist<t>(); for(t t : source) { if(predicate.ok(t)) results.add(t); } return results; }
why can't use snippet:
string strings[] = {"one","two","three"}; list<string> containingo = iterablefuncs.filter(strings,new booleantest() { public boolean ok(string obj) { return obj.indexof("o") != -1; } });
as far know, java array implements iterable, right? needs changed make function work arrays, collections? choosing iterable first parameter, figured got cases covered.
i agree, it's annoying.
you overload filter() thusly...
public static <t> list<t> filter(t[] source, booleantest predicate) { return filter(arrays.aslist(source), predicate); }
Comments
Post a Comment