JDK1.8新特性学习(一)

本文详细探讨了JDK1.8的主要新特性,包括Lambda表达式的使用、Stream API的引入、方法引用和构造器引用的优化,以及默认方法的增强。通过实例解析,展示了这些特性如何提升代码的简洁性和效率。
摘要由CSDN通过智能技术生成
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;
	}
	
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值