利用lambda过滤产品类型

利用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值