Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for Using method references on a instance to be determined at runtime in Java

Current License: CC BY-SA 3.0

17 events
when toggle format what by license comment
May 9, 2020 at 12:14 answer added GingerBeer timeline score: 0
Jan 18, 2017 at 19:19 comment added Eddie Lin @Holger That makes perfect sense now. If I change Predicate <String> predicate = String::isEmpty; to something like Predicate <String> predicate = stringInstance::isEmpty; it won't compile. Thanks for all your responses!
Jan 18, 2017 at 10:56 comment added Holger Just consider that the method reference is the equivalent to (instance, i, j) -> instance.evaluate(i, j). Three parameters.
Jan 18, 2017 at 10:54 comment added Holger @Eddie Lin: As Jorn Vernee already explained with the String::startsWith example, an instance method taking two arguments results in a function taking three arguments. Your evaluate needs an Evaluation instance and two int arguments, hence, you need a function type having three parameters. Since BiFunction only has two parameters, it is inappropriate. And since there is no predefined TriFunction interface, you have to create your own interface.
Jan 18, 2017 at 0:58 comment added Eddie Lin @JornVernee When I call it it will be something like System.out.println("predicate.test(""));right? I do not need to create a new functional interface with a method that takes in something like (Evaluation instance, String a). Why do I need to do so for my code with BiPredicate? Is BiPredicate a special case?
Jan 18, 2017 at 0:21 comment added Jorn Vernee @EddieLin You will need to do that when you call it, the test method takes 1 parameter. It's doesn't have to be new though, any instance will work (in my answer, I just used new Evaluation() as an example)
Jan 18, 2017 at 0:16 comment added Eddie Lin @JornVernee You are totally right. However, why does Predicate <String> predicate = String::isEmpty; work? Like why don't I need to do pass a new instance of String() (As your answer below to my original question may indicate)?
Jan 17, 2017 at 23:45 comment added Jorn Vernee String::startsWith would take 3 arguments; 1. the String instance on which to invoke, 2. the String parameter prefix and 3. the int parameter toffset. But a Bipredicate<String, Integer> can only account for 2 of those. String::isEmpty, takes 1 parameter, the instance to invoke on, so a Predicate<String> will work.
Jan 17, 2017 at 21:59 comment added Holger See also here
Jan 17, 2017 at 21:57 history edited Holger
edited tags
Jan 17, 2017 at 21:48 history edited Eddie Lin CC BY-SA 3.0
added 532 characters in body
Jan 17, 2017 at 21:17 answer added Lew Bloch timeline score: 0
Jan 17, 2017 at 21:01 answer added Jorn Vernee timeline score: 2
Jan 17, 2017 at 20:55 answer added Michał Szewczyk timeline score: 2
Jan 17, 2017 at 20:55 history edited Jorn Vernee CC BY-SA 3.0
formatting
Jan 17, 2017 at 20:54 review First posts
Jan 17, 2017 at 20:59
Jan 17, 2017 at 20:52 history asked Eddie Lin CC BY-SA 3.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /