利用lambad的Predicate,对支持某种功能比如蓝牙,然后返回手机的种类的集合。
1、首先需要定义一个产品的类型,以及支持的功能的枚举类。
package com.java8.filter;
/**
* 产品类型的封装,支持的功能属性
*/
public enum ElectricalType {
XIAOMI ("XIAOMI", true, true),
HUAWEI("HUAWEI", false, true),
APPLE("APPLE", false, true),
SANXING("SANXING", false, true);
String neType;
boolean bluetoothSupported;
boolean phoneSupported;
ElectricalType(String neType,boolean bluetoothSupported, boolean phoneSupported) {
this.neType = neType;
this.bluetoothSupported = bluetoothSupported;
this.phoneSupported = phoneSupported;
}
public String getNeType() {
return neType;
}
public void setNeType(String neType) {
this.neType = neType;
}
public boolean isBluetoothSupported() {
return bluetoothSupported;
}
public void setBluetoothSupported(boolean bluetoothSupported) {
this.bluetoothSupported = bluetoothSupported;
}
public boolean isPhoneSupported() {
return phoneSupported;
}
public void setPhoneSupported(boolean phoneSupported) {
this.phoneSupported = phoneSupported;
}
}
2、测试的一个main方法
package com.java8.filter;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Filter {
public static void main(String[] args) {
// qcSupportedNeTypes = Stream.of(NHCNeConfig.values())
// .filter(NHCNeConfig::isQcSupported)
// .map(NHCNeConfig::getNeType)
// .collect(Collectors.toList());
// 测试只支持蓝牙的手机类型 [XIAOMI]
getNeType(ElectricalType::isBluetoothSupported);
// 支持电话的手机类型 [XIAOMI, HUAWEI, APPLE, SANXING]
getNeType(ElectricalType::isPhoneSupported);
}
// 定义的一个统一的接口实现类
// 通过过滤条件,实现返回特定的类型结合
// 比如定义一种电器类型,通过支持的功能可以过滤相应的类型。 比如一个电器是否支持蓝牙,phone
private static List<Object> getNeType(Predicate<? super ElectricalType> f) {
List<Object> collect = Stream.of(ElectricalType.values())
.filter(f)
.map(ElectricalType::getNeType)
.collect(Collectors.toList());
System.out.println(collect);
return collect;
}
}```
getNeType