Skip to content

Instantly share code, notes, and snippets.

@codedeep79
Created July 20, 2022 09:13
Show Gist options
  • Select an option

  • Save codedeep79/d043cc1ed03cf7c7a808473b29098445 to your computer and use it in GitHub Desktop.

Select an option

Save codedeep79/d043cc1ed03cf7c7a808473b29098445 to your computer and use it in GitHub Desktop.
Ví dụ Supplier trong Java 8

In Java 8, Supplier is a functional interface; it takes no arguments and returns a result.

  • Supplier.java
@FunctionalInterface
public interface Supplier<T> {
    T get();
}

Supplier

This example uses Supplier to return a current date-time.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.function.Supplier;

public class Java8Supplier {
    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
    public static void main(String[] args) {
        Supplier<LocalDateTime> s = () -> LocalDateTime.now();
        LocalDateTime time = s.get();
        System.out.println(time);

        Supplier<String> s1 = () -> dtf.format(LocalDateTime.now());
        String time2 = s1.get();
        System.out.println(time2);
    }
}

Returns a Supplier

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class Java8Supplier {
    public static void main(String[] args) {
        System.out.println(productSupplier().get());
    }
    private static Supplier <List<Product>> productSupplier() {
        Supplier <List<Product>> productSupplier = () -> {
            List <Product> productsList = new ArrayList<Product>();
            productsList.add(new Product(1, "HP Laptop", 25000f));
            productsList.add(new Product(2, "Dell Laptop", 30000f));
            productsList.add(new Product(3, "Lenevo Laptop", 28000f));
            productsList.add(new Product(4, "Sony Laptop", 28000f));
            productsList.add(new Product(5, "Apple Laptop", 90000f));
            productsList.add(new Product(6, "Apple Laptop", 90000f));
            productsList.add(new Product(7, "Dell Laptop", 30000f));
            productsList.add(new Product(8, "Dell Laptop", 30000f));
            return productsList;
        };
        return productSupplier;
    }
}
class Product {
    private int id;
    private String name;
    private float price;

    public Product(int id, String name, float price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
    }
}

Factory

A simple factory method to return a Developer object.

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.function.Supplier;

public class Java8Supplier {
    public static void main(String[] args) {
        Developers obj = factory(Developers::new);
        System.out.println(obj);

        Developers obj2 = factory(() -> new Developers("toptech"));
        System.out.println(obj2);
    }

    public static Developers factory(Supplier<? extends Developers> s) {

        Developers developer = s.get();
        if (developer.getName() == null || "".equals(developer.getName())) {
            developer.setName("default");
        }
        developer.setSalary(BigDecimal.ONE);
        developer.setStart(LocalDate.of(2022, 8, 8));
        return developer;
    }
}
class Developers {
    String name;
    BigDecimal salary;
    LocalDate start;
    public Developers() {
    }
    public Developers(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public BigDecimal getSalary() {
        return salary;
    }

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

    public LocalDate getStart() {
        return start;
    }

    public void setStart(LocalDate start) {
        this.start = start;
    }
    @Override
    public String toString() {
        return "Developers{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", start=" + start +
                '}';
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment