import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class FilteringFoods {
public static void main(String… args) {
//only Veg. list using method reference
List<FoodItem> onlyNonVegList = Dish.stream()
.filter(FoodItem::isType)
.collect(Collectors.toList());
onlyNonVegList.forEach(System.out::println);
//filter price using lamda expression
System.out.println(“Price less than 400”);
List<FoodItem> dishPriceFilter = Dish.stream()
.takeWhile(f -> f.getPrice() < 400)
.collect(Collectors.toList());
dishPriceFilter.forEach(System.out::println);
//Drop while
System.out.println(“Price greater than 400”);
List<FoodItem> dishPriceFltr = Dish.stream()
.dropWhile(f -> f.getPrice() < 400)
.collect(Collectors.toList());
dishPriceFltr.forEach(System.out::println);
//anyMatch
boolean anyMatch = Dish.stream()
.anyMatch(FoodItem::isType);
System.out.println(“Any Match:” + anyMatch);
boolean allMatch = Dish.stream()
.anyMatch(FoodItem::isType);
System.out.println(“All Match:” + allMatch);
if(vegLover()) {
System.out.println(“Veg Lover”);
}
System.out.println(“Printing Veg list having price less than 500”);
Dish.stream()
.filter(f -> f.getVegeteraian().equals(FoodType.OTHER.toString()))
.filter(f -> f.getPrice() < 500)
.map(n -> {
System.out.println(“Name of food is:” + n.getDishName());
return “DishName:” + n.getDishName() + “ Price:” + n.getPrice();
}).limit(1)
.forEach(System.out::println);;
System.out.println(“Total Menu Price”);
int sumOfDishes = Dish.stream()
.mapToInt(FoodItem::getPrice)
.sum();
System.out.println(“sumOfDishes:” + sumOfDishes);
System.out.println(“Top Price”);
OptionalInt topprice = Dish.stream()
.mapToInt(FoodItem::getPrice)
.max();
System.out.println(“topprice:” + topprice.getAsInt());
}
public static boolean vegLover() {
boolean anyMatch = Dish.stream()
.anyMatch(FoodItem::isType);
return anyMatch;
}
public static final List<FoodItem> Dish =
Arrays.asList(
new FoodItem(“Fish Curry”, false, 120, FoodType.FISH.toString()),
new FoodItem(“Mutton Korma”, false, 220, FoodType.MEAT.toString()),
new FoodItem(“Noodles”, true, 320, FoodType.OTHER.toString()),
new FoodItem(“Paani Puri”, true, 420, FoodType.OTHER.toString()),
new FoodItem(“Mutton Masala”, false, 520, FoodType.MEAT.toString()),
new FoodItem(“Chicken Chilly”, false, 620, FoodType.MEAT.toString()),
new FoodItem(“Mutton Malai”, false, 720, FoodType.OTHER.toString())
);
public enum FoodType {
MEAT,
FISH,
OTHER
}
}
class FoodItem {
String dishName;
boolean type;
int price;
String vegeteraian;
public FoodItem(String dishName, boolean type, int price, String vegeteraian) {
super();
this.dishName = dishName;
this.type = type;
this.price = price;
this.vegeteraian = vegeteraian;
}
public String getDishName() {
return dishName;
}
public void setDishName(String dishName) {
this.dishName = dishName;
}
public boolean isType() {
return type;
}
public void setType(boolean type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getVegeteraian() {
return vegeteraian;
}
public void setVegeteraian(String vegeteraian) {
this.vegeteraian = vegeteraian;
}
@Override
public String toString() {
return “FoodItem [dishName=” + dishName + “, type=” + type + “, price=” + price + “, vegeteraian=” + vegeteraian
+ “]”;
}
}