package test;
import java.util.ArrayList;
import java.util.List;
/**
* 业务场景:商场的商品进行筛选,颜色为红色,或者 价格小于1000
*
*
*/
public class JDK8Test {
public static void main(String[] args) {
/*
* 利用工厂设计模式实现,不好的地方,如果有多个条件得写多个条件实现类
*/
List<Product> list = new ArrayList<>();
Product p1 = new Product();
p1.setProductName("帽子");
p1.setColor("purple");
p1.setPrice(50.0);
list.add(p1);
Product p2 = new Product();
p2.setProductName("太阳镜");
p2.setColor("black");
p2.setPrice(50.0);
list.add(p2);
Product p3 = new Product();
p3.setProductName("沙滩裙");
p3.setColor("red");
p3.setPrice(1000.0);
list.add(p3);
// ColorCondition colorCondition = new ColorCondition(); //颜色过滤类
PriceCondition colorCondition = new PriceCondition(); //价格获取类
List<Product> newList = filterProductByCondition(list,colorCondition);
for (Product product : newList) {
System.out.println(product.toString());
}
/*
* 使用匿名内部类
*/
List<Product> newList2 = filterProductByCondition(list,new Condition<Product>() {
private final String COLOR = "red";
@Override
public boolean test(Product t) {
if(COLOR.equals(t.getColor())){
return true;
}
return false;
}
});
for (Product product : newList2) {
System.out.println(product.toString());
}
/*
*使用lambda
*/
List<Product> products = filterProductByCondition(list, (p) -> p.getPrice() < 1000);
for (Product product : products) {
System.out.println(product.toString());
}
/*
* 使用jdk1.8中的stream API 进行集合的操作
*/
list.stream().filter((p)->p.getPrice()<1000).forEach(System.out::println);
}
public static List<Product> filterProductByCondition(List<Product> list,Condition<Product> mp){
List<Product> newList = new ArrayList<>();
for (Product product : list) {
if(mp.test(product)){
newList.add(product);
}
}
return newList;
}
}
package test;
/**
* 筛选条件接口
*
*/
public interface Condition<T> {
boolean test(T t);
public default void tt(){
System.out.println("1231");
}
}
package test;
/**
* 颜色筛选条件
*
*/
public class ColorCondition implements Condition<Product>{
private final String COLOR = "red";
@Override
public boolean test(Product t) {
if(COLOR.equals(t.getColor())){
return true;
}
return false;
}
}
package test;
/**
* 价格筛选条件
*
*/
public class PriceCondition implements Condition<Product> {
private final Double PRICE = 1000.0;
@Override
public boolean test(Product t) {
if(t.getPrice() < PRICE){
return true;
}
return false;
}
}
package test;
/**
* 商品
* @author vela_sun
*
*/
public class Product {
/**
* 商品名称
*/
private String productName;
/**
* 商品颜色
*/
private String color;
/**
* 商品价格
*/
private Double price;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [productName=" + productName + ", color=" + color + ", price=" + price + "]";
}
public Product(String productName, String color, Double price) {
super();
this.productName = productName;
this.color = color;
this.price = price;
}
public Product() {
super();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
result = prime * result + ((productName == null) ? 0 : productName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (price == null) {
if (other.price != null)
return false;
} else if (!price.equals(other.price))
return false;
if (productName == null) {
if (other.productName != null)
return false;
} else if (!productName.equals(other.productName))
return false;
return true;
}
}