Files
spring-security-series/Java/Java-stream-filter/Example2.java
2020-02-15 16:40:57 +05:30

51 lines
1.3 KiB
Java

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Customer {
private String name;
private String country;
Customer(String _name, String _country){
this.name = _name;
this.country = _country;
}
String getCountry() {
return country;
}
private String getName() {
return name;
}
static void printCustomerDetail(List<Customer> customerList){
customerList.forEach( e -> System.out.println("Customer name : "+e.getName()+" Country : "+e.getCountry()));
}
}
public class Example2 {
public static void main(String[] args) {
Map<Integer,Customer> customerMap = new HashMap<>();
String country = "UK";
customerMap.put(1, new Customer("Boris","UK"));
customerMap.put(2, new Customer("William","UK"));
customerMap.put(3, new Customer("Simon","UK"));
customerMap.put(4, new Customer("Paul","Zimbabwe"));
List<Customer> customerList = customerMap
.entrySet()
.stream()
.filter(x -> x.getValue().getCountry().equals(country))
.map(item -> item.getValue())
.collect(Collectors.toList());
Customer.printCustomerDetail(customerList);
}
}