Java Lambda Expressions Collections Example

I was wondering new features in Java 8 and wanted to play around with Lambda expressions. So I’ve eventually ended up preparing some collection examples. Hope you’ll easily summarize and understand it

The Code

package com.oracle.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class CollectionExample {

    public static void main(String[] args) {

        List<Developers> developers = new ArrayList<>(Arrays.asList(
                new Developers("Bjarne", "C++", "M", 6000),
                new Developers("Tugrul", "Java", "M", 4500),
                new Developers("James", "Java", "M", 9000),
                new Developers("Ada", "Ada", "F", 6000),
                new Developers("Diane", "C++", "F", 4000),
                new Developers("Bill", ".NOT", "M", 2000),
                new Developers("Rebecca", "C++", "F", 8500),
                new Developers("Laura", "Java", "F", 7500),
                new Developers("Adam", "Java", "M", 9800),
                new Developers("Jane", "C++", "F", 7500),
                new Developers("Mark", "Java", "M", 7500)
        )
        );
        System.out.println("\n\nList of Developers");
        System.out.println("Name\tLanguage\tGender\tSalary");
        developers.forEach(dev -> System.out.println(dev.getName() + '\t' + dev.getLanguage() + '\t' + '\t' + dev.getGender() + '\t' + dev.getSalary()));
        //Filter developers by salary 
        Predicate<Developers> salaryFilter = dev -> (dev.getSalary() >= 4500);
        //Filter developers by gender
        Predicate<Developers> genderFilter = dev -> (dev.getGender().equals("M"));
        //Filter developers by language
        Predicate<Developers> languageFilter = dev -> (dev.getLanguage().equals("Java"));
        System.out.println("\n\nList of Developers - Sort by Male Java Developers earn 4500 and more");
        System.out.println("Name\tLanguage\tGender\tSalary");
        //Apply filter
        developers.stream()
                .filter(salaryFilter)
                .filter(genderFilter)
                .filter(languageFilter)
                .forEach(dev -> System.out.println(dev.getName() + '\t' + dev.getLanguage() + '\t' + '\t' + dev.getGender() + '\t' + dev.getSalary()));

        System.out.println("\n\nList of Developers - Limited to 3 of them");
        System.out.println("Name\tLanguage\tGender\tSalary");
        //Apply filter
        developers.stream()
                .limit(3)
                .forEach(dev -> System.out.println(dev.getName() + '\t' + dev.getLanguage() + '\t' + '\t' + dev.getGender() + '\t' + dev.getSalary()));

        System.out.println("\n\nList of Developers - Sorted by Name Ascending");
        System.out.println("Name\tLanguage\tGender\tSalary");
        //Apply filter
        developers.stream()
                .sorted((dev1, dev2) -> (dev1.getName().compareTo(dev2.getName())))
                .forEach(dev -> System.out.println(dev.getName() + '\t' + dev.getLanguage() + '\t' + '\t' + dev.getGender() + '\t' + dev.getSalary()));

        System.out.println("\n\nList of Developers - Sorted by Salary Ascending");
        System.out.println("Name\tLanguage\tGender\tSalary");
        //Apply filter
        developers.stream()
                .sorted((dev1, dev2) -> (dev1.getSalary() - dev2.getSalary()))
                .forEach(dev -> System.out.println(dev.getName() + '\t' + dev.getLanguage() + '\t' + '\t' + dev.getGender() + '\t' + dev.getSalary()));

        System.out.println("\n\nThe Developer earning the lowest salary");
        System.out.println("Name\tLanguage\tGender\tSalary");
        //Apply filter
        Developers developerMin = developers.stream()
                .min((dev1, dev2) -> (dev1.getSalary() - dev2.getSalary()))
                .get();
        System.out.println(developerMin.getName() + '\t' + developerMin.getLanguage() + '\t' + '\t' + developerMin.getGender() + '\t' + developerMin.getSalary());

        System.out.println("\n\nThe Developer earning the highest salary");
        System.out.println("Name\tLanguage\tGender\tSalary");
        //Apply filter
        Developers developerMax = developers.stream()
                .max((dev1, dev2) -> (dev1.getSalary() - dev2.getSalary()))
                .get();
        System.out.println(developerMax.getName() + '\t' + developerMax.getLanguage() + '\t' + '\t' + developerMax.getGender() + '\t' + developerMax.getSalary());

    }

}

class Developers {

    private String name;
    private String language;
    private String gender;
    private int salary;

    public Developers(String name, String language, String gender, int salary) {
        this.name = name;
        this.language = language;
        this.gender = gender;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

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

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getGender() {
        return gender;
    }

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

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

}

The output

List of Developers
Name	Language	Gender	Salary
Bjarne	C++		M	6000
Tugrul	Java		M	4500
James	Java		M	9000
Ada	Ada		F	6000
Diane	C++		F	4000
Bill	.NOT		M	2000
Rebecca	C++		F	8500
Laura	Java		F	7500
Adam	Java		M	9800
Jane	C++		F	7500
Mark	Java		M	7500


List of Developers - Sort by Male Java Developers earn 4500 and more
Name	Language	Gender	Salary
Tugrul	Java		M	4500
James	Java		M	9000
Adam	Java		M	9800
Mark	Java		M	7500


List of Developers - Limited to 3 of them
Name	Language	Gender	Salary
Bjarne	C++		M	6000
Tugrul	Java		M	4500
James	Java		M	9000


List of Developers - Sorted by Name Ascending
Name	Language	Gender	Salary
Ada	Ada		F	6000
Adam	Java		M	9800
Bill	.NOT		M	2000
Bjarne	C++		M	6000
Diane	C++		F	4000
James	Java		M	9000
Jane	C++		F	7500
Laura	Java		F	7500
Mark	Java		M	7500
Rebecca	C++		F	8500
Tugrul	Java		M	4500


List of Developers - Sorted by Salary Ascending
Name	Language	Gender	Salary
Bill	.NOT		M	2000
Diane	C++		F	4000
Tugrul	Java		M	4500
Bjarne	C++		M	6000
Ada	Ada		F	6000
Laura	Java		F	7500
Jane	C++		F	7500
Mark	Java		M	7500
Rebecca	C++		F	8500
James	Java		M	9000
Adam	Java		M	9800


The Developer earning the lowest salary
Name	Language	Gender	Salary
Bill	.NOT		M	2000


The Developer earning the highest salary
Name	Language	Gender	Salary
Adam	Java		M	9800

Leave a Reply

Your email address will not be published. Required fields are marked *