Added java stream examples

This commit is contained in:
NKaushik89
2020-02-15 16:40:57 +05:30
parent 0aa65669c7
commit e33858720c
3 changed files with 165 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
enum OrderType {
ONLINE,
OFFLINE
}
class Order {
private String customerName;
private OrderType orderType;
private float amount;
Order(OrderType _orderType, float _amount, String _customerName){
this.orderType = _orderType;
this.amount = _amount;
this.customerName = _customerName;
}
public OrderType getOrderType() {
return orderType;
}
public float getAmount() {
return amount;
}
public String getCustomerName() {
return customerName;
}
static void printOrders(List<Order> orderList){
orderList.forEach(e -> {
System.out.println("Customer Name : "+e.getCustomerName()+", Order type : "+e.getOrderType()+" , Amount : "+e.getAmount());
});
}
}
public class Example1 {
public static void main(String[] args) {
Order[] orderArray = {new Order(OrderType.OFFLINE, 1000, "Bob"), new Order(OrderType.ONLINE, 500, "Rose"),
new Order(OrderType.OFFLINE, 400, "Nancy"), new Order(OrderType.ONLINE, 1500, "Kate")};
List<Order> orderList = Arrays.asList(orderArray);
System.out.println("Given Orders : ");
Order.printOrders(orderList);
List<Order> offlineList = orderList.stream()
.filter(item -> item.getOrderType().equals(OrderType.OFFLINE))
.collect(Collectors.toList());
System.out.println("Offline order list : ");
Order.printOrders(offlineList);
List<Order> belowThousandList = orderList.stream()
.filter(item -> item.getAmount() < 1000)
.collect(Collectors.toList());
System.out.println("Orders with less than 1000 : ");
Order.printOrders(belowThousandList);
}
}

View File

@@ -0,0 +1,50 @@
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);
}
}

View File

@@ -0,0 +1,50 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
class Student {
private String name;
private int marks;
Student(String _name, int _marks){
this.name = _name;
this.marks = _marks;
}
String getName() {
return name;
}
int getMarks() {
return marks;
}
static void printStudents(List<Student> studentList){
studentList.forEach( e -> System.out.println("Student name : "+e.getName()+" Marks : "+e.getMarks()));
}
}
public class Example3 {
public static void main(String[] args) {
Student[] studentArray = {new Student("Jacob", 80), new Student("Amanda", 70),
new Student("Ashley", 85), new Student("Alexis", 90)};
List<Student> studentList = Arrays.asList(studentArray);
Predicate<Student> studentFilterPredicate = new Predicate<Student>() {
@Override
public boolean test(Student student) {
return student.getName().startsWith("A") && student.getMarks() > 80;
}
};
List<Student> studentAList = studentList.stream()
.filter(studentFilterPredicate)
.collect(Collectors.toList());
Student.printStudents(studentAList);
}
}