Created
June 8, 2018 12:09
-
-
Save vakho10/806a206db3bb0a7df2c8999cb2f07c44 to your computer and use it in GitHub Desktop.
Example demonstrating Lambdas in Java
This file contains 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
package ge.tsu.lambdas; | |
import java.util.function.Consumer; | |
import java.util.function.Predicate; | |
public class Main { | |
public static void main(String... args) { | |
Person kaiKaci = new Person("Nikoloz", "Grdzelidze", 28); | |
Person pataraKaci = new Person("Giorgi", "Asatiani", 17); | |
Person amuricanMan = new Person("John" ,"Smith", 12); | |
Amurica amurica = new Amurica(); | |
Georgia georgia = new Georgia(); | |
System.out.println("Georgians:"); | |
System.out.println(kaiKaci); | |
System.out.println(pataraKaci); | |
System.out.println("Amuricans:"); | |
System.out.println(amuricanMan); | |
// Check ages | |
georgia.tellMeIfMyAgeIsLegal(kaiKaci); | |
georgia.tellMeIfMyAgeIsLegal(pataraKaci); | |
amurica.tellMeIfMyAgeIsLegal(amuricanMan); | |
System.out.println(); | |
// Change surnames | |
georgia.changeSurnameTo(kaiKaci, "Grdzelishvili"); | |
georgia.changeSurnameTo(pataraKaci, pataraKaci.getLastname().substring(0, pataraKaci.getLastname().length() - 1).concat("oglu")); | |
amurica.changeSurnameTo(amuricanMan, "Brown"); | |
System.out.println("Georgians:"); | |
System.out.println(kaiKaci); | |
System.out.println(pataraKaci); | |
System.out.println("Amuricans:"); | |
System.out.println(amuricanMan); | |
} | |
} | |
class Person { | |
private String firstname; | |
private String lastname; | |
private int age; | |
public Person() { | |
} | |
public Person(String firstname, String lastname, int age) { | |
this.firstname = firstname; | |
this.lastname = lastname; | |
this.age = age; | |
} | |
public String getFirstname() { | |
return firstname; | |
} | |
public void setFirstname(String firstname) { | |
this.firstname = firstname; | |
} | |
public String getLastname() { | |
return lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
return "Person [firstname=" + firstname + ", lastname=" + lastname + ", age=" + age + "]"; | |
} | |
} | |
interface Countryable { | |
public abstract void changeSurnameTo(Person person, String newSurname); | |
public abstract void tellMeIfMyAgeIsLegal(Person person); | |
} | |
class Georgia implements Countryable { | |
@Override | |
public void changeSurnameTo(Person person, String newSurname) { | |
Country.changeSurnameWithConsumer(person, c -> c.setLastname(newSurname)); | |
} | |
@Override | |
public void tellMeIfMyAgeIsLegal(Person person) { | |
// Call helper method :) | |
Country.printHasLegalAge(person, p -> p.getAge() > 18); // Pass georgian predicate here! | |
} | |
} | |
class Amurica implements Countryable { | |
@Override | |
public void changeSurnameTo(Person person, String newSurname) { | |
// All Amuricans add middlenames of President Trump :| | |
Country.changeSurnameWithConsumer(person, c -> c.setLastname("J. " + newSurname)); | |
} | |
@Override | |
public void tellMeIfMyAgeIsLegal(Person person) { | |
// Every Amurican drinks at any age :| | |
Country.printHasLegalAge(person, p -> { | |
return p.getAge() > 0; | |
}); | |
} | |
} | |
final class Country { | |
static <T> void changeSurnameWithConsumer(T someone, Consumer<T> surnameChanger) { | |
surnameChanger.accept(someone); | |
} | |
static <T> void printHasLegalAge(T someone, Predicate<T> predicate) { | |
if (predicate.test(someone)) { | |
System.out.println(String.format("%s has legal age. :)", someone)); | |
} else { | |
System.out.println(String.format("%s has illegal age! :(", someone)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment