✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page

Was this helpful?

  1. Java
  2. Useful Stuff Java

Lambda & Stream


@FunctionalInterface
interface CheckPerson {
    boolean test(Person p);
}

@FunctionalInterface
interface Printer {
    void test(Object o);
}

class CheckPersonWithCondition implements CheckPerson {

    @Override
    public boolean test(Person p) {
        return p.gender == Person.Gender.MALE;
    }

}

class ObjectPrinter implements Printer {

    @Override
    public void test(Object o) {
        System.out.println(o.toString());
    }
}

public class App {

    public static void main(String[] args) throws Exception {

        List<Person> peoples = Person.generatePersonList();

        System.out.println(peoples);

        // Condition for Male
        printPeopleWithCheck(peoples, new CheckPersonWithCondition());

        // Condition for Female
        printPeopleWithCheck(peoples, new CheckPerson() {
            @Override
            public boolean test(Person p) {
                return p.gender == Person.Gender.FEMALE;
            }
        });

        // With Lambda Expression for Male
        printPeopleWithCheck(peoples, p -> p.gender == Person.Gender.MALE);

        // With Predicatefor Female
        printPeopleWithCheckWithPredicate(peoples, p -> p.gender == Person.Gender.FEMALE);

        List<Person> personList = Person.generatePersonList();

        personList.forEach(System.out::println);

        List<Person> newList = personList.stream().map(
                x -> new Person(
                        x.getName(),
                        x.getAge() + 1,
                        x.getEmailAddress(),
                        x.getGender()))
                .collect(Collectors.toList());

        List<Person> anotherList = personList.stream()
                .filter(x -> x.getGender() == Person.Gender.FEMALE)
                .collect(Collectors.toList());

        List<Person> randomList = personList.stream()
                .sorted(Comparator.comparing(Person::getAge))
                .collect(Collectors.toList());

        System.out.println(newList);
        System.out.println(anotherList);
        System.out.println(randomList);

        ObjectPrinter objPrinter = new ObjectPrinter();
        objPrinter.test(10);

        App.appPrinter(10, System.out::println);

        Runnable runnable = () -> {
            System.out.println("hello world");
        };

        new Thread(runnable).start();
        System.out.println("another hello");

        Printer printer = (i) -> {
            System.out.println("Number is : " + i);
        };

        printer.test(21);

        List<Integer> arrlist = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<Integer> evenList = arrlist.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
        List<Integer> revSortedList = arrlist.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        List<Integer> naturalSortedList = revSortedList.stream().sorted(Comparator.naturalOrder())
                .collect(Collectors.toList());

        System.out.println(evenList);
        System.out.println(revSortedList);
        System.out.println(naturalSortedList);

    }

    public static void printPeopleWithCheck(List<Person> people, CheckPerson checkPerson) {
        for (Person p : people) {
            if (checkPerson.test(p)) {
                p.printPerson();
            }
        }
    }

    public static void printPeopleWithCheckWithPredicate(List<Person> people, Predicate<Person> checkPerson) {
        for (Person p : people) {
            if (checkPerson.test(p)) {
                p.printPerson();
            }
        }
    }

    public static void appPrinter(Object o, Printer genericPrinter) {
        genericPrinter.test(o);
    }
}

// Person.java
import java.util.ArrayList;
import java.util.List;

class Person {

    public enum Gender {
        MALE, FEMALE
    }

    Gender gender;

    String name;
    int age;
    String emailAddress;

    public Person() {
    }

    public Person(String name, int age, String emailAddress, Gender gender) {
        this.name = name;
        this.age = age;
        this.emailAddress = emailAddress;
        this.gender = gender;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    @Override
    public String toString() {
        return "Person{" +
                "gender=" + gender +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", emailAddress='" + emailAddress + '\'' +
                '}';
    }

    public void printPerson() {
        System.out.println(this.toString());
    }

    public static List<Person> generatePersonList() {
        List<Person> people = new ArrayList<>();

        people.add(new Person("Uday Yadav", 21, "udayyadav@earth.com", Gender.MALE));
        people.add(new Person("Another Uday", 22, "yadavuday@earth.com", Gender.MALE));
        people.add(new Person("Multiverse Uday", 19, "multiuday@muultiverse.com", Gender.FEMALE));

        return people;
    }

}

PreviousUseful Stuff JavaNextKeywords in Java

Last updated 2 years ago

Was this helpful?