描述:此次笔记以代码优化为侧重点编写,主要举例子处理疑惑
本次举例有:简单if语句判断、编写接口工厂判断、编写匿名内部类判断、编写lambda格式判断(条件:只有一个抽象方法前提)、函数式接口(Java1.8新特性)
例子主要工作:从list中过滤掉不是红色、价格小于8000的数据。(复制粘贴导包即可运行测试)
1、准备工作、创建bean类(全部例子通用)
public class Product {
private String color;
private int price;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Product(String color, int price) {
this.color = color;
this.price = price;
}
}
2、简单if语句判断、测试类
public class filter {
// 筛选颜色为红色
public static List<Product> filterProductByColor(List<Product> list){
List<Product> prods = new ArrayList<>();
for (Product product : list){
if ("红色".equals(product.getColor())){
prods.add(product);
}
}
return prods;
}
// 筛选价格大于8千的
public static List<Product> filterProductByPrice(List<Product> list){
List<Product> prods = new ArrayList<>();
for (Product product : list){
if (product.getPrice() > 8000){
prods.add(product);
}
}
return prods;
}
//主函数
public static void main(String[] args) {
List<Product> prods = new ArrayList<>();
prods.add(new Product("黄色", 100));
prods.add(new Product("红色", 100));
prods.add(new Product("红色", 10000));
prods.add(new Product("黄色", 10000));
prods = filterProductByColor(prods);
prods = filterProductByPrice(prods);
prods.stream().forEach(item->System.out.println(item.getColor()+":"+item.getPrice()));
}
}
3、编写接口工厂判断
接口编写
public interface MyPredicate<T> {
boolean test(T t);
}
两个接口实现类
public class ColorPredicate implements MyPredicate <Product> {
private static final String RED = "红色";
@Override
public boolean test(Product product) {
return RED.equals(product.getColor());
}
}
public class PricePredicate implements MyPredicate<Product> {
@Override
public boolean test(Product product) {
return product.getPrice() > 8000;
}
}
测试类
public class filter {
//过滤方法
public static List<Product> filterM(List<Product> list,MyPredicate<Product> mp){
List<Product> prods = new ArrayList<>();
for (Product prod : list){
if (mp.test(prod)){
prods.add(prod);
}
}
return prods;
}
//主函数
public static void main(String[] args) {
List<Product> prods = new ArrayList<>();
prods.add(new Product("黄色", 100));
prods.add(new Product("红色", 100));
prods.add(new Product("红色", 10000));
prods.add(new Product("黄色", 10000));
prods = filterM(prods, new ColorPredicate());
prods = filterM(prods, new PricePredicate());
prods.stream().forEach(item->System.out.println(item.getColor()+":"+item.getPrice()));
}
}
4、编写匿名内部类判断
接口编写
public interface MyPredicate<T> {
boolean test(T t);
}
测试类
public class filter {
//过滤方法
public static List<Product> filterProductByPredicate(List<Product> list,MyPredicate<Product> mp){
List<Product> prods = new ArrayList<>();
for (Product prod : list){
if (mp.test(prod)){
prods.add(prod);
}
}
return prods;
}
//主函数
public static void main(String[] args) {
List<Product> prods = new ArrayList<>();
prods.add(new Product("黄色", 100));
prods.add(new Product("红色", 100));
prods.add(new Product("红色", 10000));
prods.add(new Product("黄色", 10000));
prods = filterProductByPredicate(prods, new MyPredicate<Product>() {
@Override
public boolean test(Product product) {
// TODO Auto-generated method stub
return "红色".equals(product.getColor());
}
});
prods = filterProductByPredicate(prods, new MyPredicate<Product>() {
@Override
public boolean test(Product product) {
// TODO Auto-generated method stub
return product.getPrice() > 8000;
}
});
prods.stream().forEach(item->System.out.println(item.getColor()+":"+item.getPrice()));
}
}
5、编写lambda格式判断
接口
public interface MyPredicate<T> {
boolean test(T t);
}
测试类
public class filter {
//过滤方法
public static List<Product> filterProductByPredicate(List<Product> list,MyPredicate<Product> mp){
List<Product> prods = new ArrayList<>();
for (Product prod : list){
if (mp.test(prod)){
prods.add(prod);
}
}
return prods;
}
//主函数
public static void main(String[] args) {
List<Product> prods = new ArrayList<>();
prods.add(new Product("黄色", 100));
prods.add(new Product("红色", 100));
prods.add(new Product("红色", 10000));
prods.add(new Product("黄色", 10000));
prods = filterProductByPredicate(prods, (product) ->"红色".equals(product.getColor()));
prods = filterProductByPredicate(prods, (product) ->product.getPrice() > 8000);
prods.stream().forEach(item->System.out.println(item.getColor()+":"+item.getPrice()));
}
}
6、函数式接口方式(不再提供自建接口,而是jdk1.8新特性自带的接口Predicate)
测试类
public class filter {
//过滤方法
public static List<Product> filterProductByPredicate(List<Product> list,Predicate<Product> mp){
List<Product> prods = new ArrayList<>();
for (Product prod : list){
if (mp.test(prod)){
prods.add(prod);
}
}
return prods;
}
//主函数
public static void main(String[] args) {
List<Product> prods = new ArrayList<>();
prods.add(new Product("黄色", 100));
prods.add(new Product("红色", 100));
prods.add(new Product("红色", 10000));
prods.add(new Product("黄色", 10000));
prods = filterProductByPredicate(prods, (product) ->"红色".equals(product.getColor()));
prods = filterProductByPredicate(prods, (product) ->product.getPrice() > 8000);
prods.stream().forEach(item->System.out.println(item.getColor()+":"+item.getPrice()));
}
}
希望对大家有所帮助,有需要纠正地方请下面留言,大家一起深讨,谢谢!!!