Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38957 > unrolled thread
| Started by | gk <srcjnu@gmail.com> |
|---|---|
| First post | 2019-05-10 21:58 -0700 |
| Last post | 2019-05-12 10:22 -0400 |
| Articles | 6 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-10 21:58 -0700
Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-11 06:31 -0400
Re: assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-11 05:57 -0700
Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-11 09:38 -0400
Re: assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-12 06:28 -0700
Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-12 10:22 -0400
| From | gk <srcjnu@gmail.com> |
|---|---|
| Date | 2019-05-10 21:58 -0700 |
| Subject | assign lambda to functional interface |
| Message-ID | <6594e2c1-ec44-4cbc-b6fb-2c8a852fa7dd@googlegroups.com> |
Can we assign a expression into an interface like this ?
import java.util.function.Predicate;
public class Main {
public static void main(String[] args)
{
// Creating predicate
Predicate<Integer> lesserthan = i -> (i < 18); // is it valid?
// Calling Predicate method
System.out.println(lesserthan.test(10));
}
}
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2019-05-11 06:31 -0400 |
| Message-ID | <qb689k$f0s$1@dont-email.me> |
| In reply to | #38957 |
On 5/11/2019 12:58 AM, gk wrote:
> Can we assign a expression into an interface like this ?
> [... code snipped ...]
Yes. Did something go wrong when you tried it?
--
esosman@comcast-dot-net.invalid
Six hundred twenty-one days to go.
[toc] | [prev] | [next] | [standalone]
| From | gk <srcjnu@gmail.com> |
|---|---|
| Date | 2019-05-11 05:57 -0700 |
| Message-ID | <4a576120-782d-4519-bee1-14f1c85c9125@googlegroups.com> |
| In reply to | #38958 |
On Saturday, May 11, 2019 at 4:01:26 PM UTC+5:30, Eric Sosman wrote: > On 5/11/2019 12:58 AM, gk wrote: > > Can we assign a expression into an interface like this ? > > [... code snipped ...] > > Yes. Did something go wrong when you tried it? > > -- > esosman@comcast-dot-net.invalid > Six hundred twenty-one days to go. This works . But that is a weird looking syntax. .... an expression going into interface which was not there in jdk 1.6 and below. also , can we do the same assignment to this to consumer also ? Consumer c = i -> (i < 18); // Consumer is functional interface. In fact can we assign this to any interface as well (non functional interface) ?
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2019-05-11 09:38 -0400 |
| Message-ID | <qb6j84$acf$1@dont-email.me> |
| In reply to | #38959 |
On 5/11/2019 8:57 AM, gk wrote:
> On Saturday, May 11, 2019 at 4:01:26 PM UTC+5:30, Eric Sosman wrote:
>> On 5/11/2019 12:58 AM, gk wrote:
>>> Can we assign a expression into an interface like this ?
>>> [... code snipped ...]
>>
>> Yes. Did something go wrong when you tried it?
>>
>> --
>> esosman@comcast-dot-net.invalid
>> Six hundred twenty-one days to go.
>
> This works . But that is a weird looking syntax. .... an expression going into interface which was not there in jdk 1.6 and below.
Is it "weird" that newer versions of Java add capabilities
not present before?
> also , can we do the same assignment to this to consumer also ?
>
> Consumer c = i -> (i < 18); // Consumer is functional interface.
... but its accept() method is void, so a boolean-valued
expression can't implement it. A different lambda could work.
> In fact can we assign this to any interface as well (non functional interface) ?
You can use a lambda expression as an instance of any functional
interface, because an F.I. has only one method and thus Java can
figure out which method the lambda implements.
You can use a lambda for a multi-method interface provided the
interface has exactly one abstract method and all the others have
`default' implementations. You can use a lambda for Consumer's
accept() method (because add() has a `default' version), but you
couldn't use a lambda for List.
--
esosman@comcast-dot-net.invalid
Six hundred twenty-one days to go.
[toc] | [prev] | [next] | [standalone]
| From | gk <srcjnu@gmail.com> |
|---|---|
| Date | 2019-05-12 06:28 -0700 |
| Message-ID | <063ded09-67e2-4750-ad8f-7fd00a210861@googlegroups.com> |
| In reply to | #38957 |
On Saturday, May 11, 2019 at 10:28:36 AM UTC+5:30, gk wrote:
> Can we assign a expression into an interface like this ?
>
> import java.util.function.Predicate;
>
> public class Main {
> public static void main(String[] args)
> {
> // Creating predicate
> Predicate<Integer> lesserthan = i -> (i < 18); // is it valid?
>
> // Calling Predicate method
> System.out.println(lesserthan.test(10));
> }
> }
Does lambda expression always have to return boolean ? or lambda can return anything ?
most example shows boolean return by lambda expression.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2019-05-12 10:22 -0400 |
| Message-ID | <qb9a6m$mec$1@dont-email.me> |
| In reply to | #38961 |
On 5/12/2019 9:28 AM, gk wrote:
>
> Does lambda expression always have to return boolean ? or lambda can return anything ?
> most example shows boolean return by lambda expression.
A lambda expression can be of any type, including void.
Your questions about lambdas are rather elementary, and suggest
that you've picked up some incomplete or even erroneous information
about them. I strongly suggest you engage in some study on your own
before going much further. A good place to start is
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
There you will find many examples of lambdas that yield booleans,
but also of lambdas that yield other types and of void lambdas.
... and if the Tutorial leaves something unanswered, many of your
questions can be resolved with the Try It For Yourself algorithm ...
--
esosman@comcast-dot-net.invalid
Six hundred nineteen days to go.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web