Created
October 7, 2017 00:00
-
-
Save shailrshah/af68e689362f56e79b06b8ca4380be60 to your computer and use it in GitHub Desktop.
An example to show how lambda expressions are mapped to the abstract mentods in functional interfaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FunctionalInterFaceExample { | |
/* Each lambda corresponds to a given type, specified by an interface. | |
A so called functional interface must contain exactly one abstract method declaration. | |
Each lambda expression of that type will be matched to this abstract method. */ | |
@FunctionalInterface | |
interface Converter<F, T> { | |
T convert(F from); | |
} | |
public static void main(String[] args) { | |
Converter<Double, Double> celciusToFarenheit = c -> (1.8*c + 32); | |
Converter<Double, Double> farenheitToCelcius = f -> (f-32) / 1.8; | |
Converter<Double, Double> celciusToKelvin = c -> c + 273.15; | |
Converter<Double, Double> KelvinToCelcius = k -> k - 273.15; | |
Converter<Double, Double> farenheitToKelvin = f -> (f + 459.67) + 5/9; | |
Converter<Double, Double> kelvinToFarenheit = k -> k * 9/5 - 459.67; | |
Converter<Integer, Double> intToDouble = i -> i + 0.00; | |
System.out.println(farenheitToCelcius.convert(60.00)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment