Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36707 > unrolled thread

Lambda function that takes no parameter and returns String

Started byCecil Westerhof <Cecil@decebal.nl>
First post2017-06-24 03:56 +0200
Last post2017-06-23 22:18 -0400
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Lambda function that takes no parameter and returns String Cecil Westerhof <Cecil@decebal.nl> - 2017-06-24 03:56 +0200
    Re: Lambda function that takes no parameter and returns String Arne Vajhøj <arne@vajhoej.dk> - 2017-06-23 22:15 -0400
      Re: Lambda function that takes no parameter and returns String Arne Vajhøj <arne@vajhoej.dk> - 2017-06-23 22:18 -0400

#36707 — Lambda function that takes no parameter and returns String

FromCecil Westerhof <Cecil@decebal.nl>
Date2017-06-24 03:56 +0200
SubjectLambda function that takes no parameter and returns String
Message-ID<87wp82m9mr.fsf@munus.decebal.nl>
I want to start with lambda functions. How would I write a lambda
function that does not take a parameter and returns a String? And how
do I ascribe that lambda function to a variable?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#36708

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-06-23 22:15 -0400
Message-ID<oikhvv$152f$1@gioia.aioe.org>
In reply to#36707
On 6/23/2017 9:56 PM, Cecil Westerhof wrote:
> I want to start with lambda functions. How would I write a lambda
> function that does not take a parameter and returns a String? And how
> do I ascribe that lambda function to a variable?

Example:

public class LambdaFun {
	@FunctionalInterface
	public static interface F {
		public String get();
	}
	public static void test(F f) {
		System.out.println(f.get());
	}
	public static void main(String[] args) {
		test(() -> "one");
		test(() -> "two");
		test(() -> "three");
		F four = () -> "four";
		test(four);
	}
}

Arne

[toc] | [prev] | [next] | [standalone]


#36709

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-06-23 22:18 -0400
Message-ID<oiki60$15b5$1@gioia.aioe.org>
In reply to#36708
On 6/23/2017 10:15 PM, Arne Vajhøj wrote:
> On 6/23/2017 9:56 PM, Cecil Westerhof wrote:
>> I want to start with lambda functions. How would I write a lambda
>> function that does not take a parameter and returns a String? And how
>> do I ascribe that lambda function to a variable?
> 
> Example:
> 
> public class LambdaFun {
>      @FunctionalInterface
>      public static interface F {
>          public String get();
>      }
>      public static void test(F f) {
>          System.out.println(f.get());
>      }
>      public static void main(String[] args) {
>          test(() -> "one");
>          test(() -> "two");
>          test(() -> "three");
>          F four = () -> "four";
>          test(four);
>      }
> }

In this specific case you do not need to define your own functional
interface as Java comes with a usable one:

import java.util.function.Supplier;

public class LambdaFun2 {
	public static void test(Supplier<String> f) {
		System.out.println(f.get());
	}
	public static void main(String[] args) {
		test(() -> "one");
		test(() -> "two");
		test(() -> "three");
		Supplier<String> four = () -> "four";
		test(four);
	}
}

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web