Created
August 24, 2016 12:23
-
-
Save valtoni/7be5c2a4612b81d2f05db284fce0d14f to your computer and use it in GitHub Desktop.
Adjustment over excelent tutorial about lambda in java 8: https://blog.idrsolutions.com/2015/02/java-8-method-references-explained-5-minutes/
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
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.function.Function; | |
| public class ReferenceToConstructor { | |
| /** | |
| * @param args the command line arguments | |
| */ | |
| public static void main(String[] args) { | |
| // TODO code application logic here | |
| List<Integer> numbers = Arrays.asList(4, 9, 16, 25, 36); | |
| List<Double> squaredNumbers = ReferenceToConstructor.findSquareRoot(numbers, Double::new); | |
| System.out.println("Square root of numbers = "+squaredNumbers); | |
| } | |
| private static List<Double> findSquareRoot(List<Integer> list, Function<Double, Double> f) { | |
| List<Double> result = new ArrayList<>(); | |
| list.forEach(x -> result.add(f.apply(Math.sqrt(x)))); | |
| return result; | |
| } | |
| } |
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
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.function.Function; | |
| public class ReferenceToInstanceMethodAOPT { | |
| /** | |
| * @param args | |
| * the command line arguments | |
| */ | |
| private static class Person { | |
| private final String name; | |
| private final int age; | |
| public Person(String name, int age) { | |
| this.name = name; | |
| this.age = age; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| } | |
| public static void main(String[] args) { | |
| // TODO code application logic here | |
| List<Person> persons = new ArrayList<>(); | |
| persons.add(new Person("Albert", 80)); | |
| persons.add(new Person("Ben", 15)); | |
| persons.add(new Person("Charlote", 20)); | |
| persons.add(new Person("Dean", 6)); | |
| persons.add(new Person("Elaine", 17)); | |
| List<Integer> allAges = ReferenceToInstanceMethodAOPT.listAllAges(persons, Person::getAge); | |
| System.out.println("Printing out all ages \n"+allAges); | |
| } | |
| private static List<Integer> listAllAges(List<Person> person, Function<Person, Integer> f) { | |
| List<Integer> result = new ArrayList<>(); | |
| person.forEach(x -> result.add(f.apply(x))); | |
| return result; | |
| } | |
| } |
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
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.function.Predicate; | |
| public class ReferenceToStaticMethodExample { | |
| /** | |
| * @param args the command line arguments | |
| */ | |
| public static void main(String[] args) { | |
| List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16); | |
| List<Integer> primeNumbers = findPrimeNumbers(numbers, (number) -> isPrime((int) number)); | |
| System.out.println("Prime Numbers are " + primeNumbers); | |
| } | |
| public static boolean isPrime(int number) { | |
| if (number == 1) { | |
| return false; | |
| } | |
| for (int i = 2; i < number; i++) { | |
| if (number % i == 0) { | |
| return false; | |
| } } | |
| return true; | |
| } | |
| public static <T> List<T> findPrimeNumbers(List<T> list, Predicate<T> predicate) { | |
| List<T> sortedNumbers = new ArrayList<>(); | |
| list.stream() | |
| .filter(i -> predicate.test(i)) | |
| .forEach(i -> sortedNumbers.add(i)); | |
| return sortedNumbers; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment