java - Passing and casting a Comparator as argument of method using Lambdas -


how can create method accept comparator wildcard argument, , capture class , use comparator ?

public list<string> myfilter(comparator<?> comp, object value, class c){    return mylist.stream()          .filter(s->               comp.compare(c.cast(s), c.cast(value))>=0           ).collect(tolist());   } 

edit: ok, in fact, andrew's answer interesting , works quite :) yet,given comments, should want is: instead of trying cast within method, expect cast had been done within comparator

so give:

  public list<string> filter(comparator<string> comp){          return mylist.filter(s->comp.compare(s, null)>=0).collect(tolist());    } 

and when calling it, should like

   list<string> mynewlist= filter((string s1, string s2)->{                   try{                       int i=integer.parseint(s1);                       return integer.compare(i, myrealcomparevalue);                  }catch(exception e){                       e.printstacktrace();                      return null;                  }); 

it doesn't nice though....

in fact, question possible duplicate of how define method takes lambda parameter in java 8?

and wanted

 public list<string> myfilter(mycomparator c){         return mylist            .stream()            .filter(c::compare)            .collect(tolist());  } 

with , interface mycomparator:

  public interface mycomparator{         public boolean compare(string s);   } 

and called:

   list<string> mynewlist=filter((string s)->s.compareto(whateverstring)>=0); 

or even:

   list<string> mynewlist=filter((string s)->{            try{                return integer.compare(integer.parseint(s), 10)>=0;            }catch(exception e){                return false;            }); 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -