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


Groups > comp.lang.java.programmer > #36709

Re: Lambda function that takes no parameter and returns String

Path csiph.com!aioe.org!.POSTED!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Lambda function that takes no parameter and returns String
Date Fri, 23 Jun 2017 22:18:30 -0400
Organization Aioe.org NNTP Server
Lines 46
Message-ID <oiki60$15b5$1@gioia.aioe.org> (permalink)
References <87wp82m9mr.fsf@munus.decebal.nl> <oikhvv$152f$1@gioia.aioe.org>
NNTP-Posting-Host PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0
X-Notice Filtered by postfilter v. 0.8.2
Content-Language en-US
Xref csiph.com comp.lang.java.programmer:36709

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web