List 多条件,多条件筛选集合

public List getState(int deviceType, String precinctId,
                                            String deviceName, String onLine) {
        List deviceList = cfgDeviceMapper.findAllDevice();//查所有
        StringBuffer s = new StringBuffer();
        Map deviceMap = new HashMap<>();//以indexCode为KEY
        for (CfgDevice device1 : deviceList) {
            s.append(device1.getUserDeviceId() + ",");
            deviceMap.put(device1.getUserDeviceId(), device1);
        }
        //查所有海康的设备状态
        String hkState = getHKState(s.toString(), ITF_ADDRESS_POST_RESULT_STATE);
        Gson gson = new Gson();
        HKVqdResult vqdResult = gson.fromJson(hkState, new TypeToken>() {
        }.getType());
        //根据查询条件放在集合里
        List deviceStatePojoVoList = new ArrayList();
        for (DeviceState deviceState : vqdResult.getData()) {
            String indexCode = deviceState.getIndexCode();
            //根据KEY取出对应的对象
            CfgDevice cfgDevice = deviceMap.get(indexCode);
            //封装
            DeviceStatePojoVo deviceStatePojoVo = new DeviceStatePojoVo();
            deviceStatePojoVo.setPrecinctName(cfgDevice.getPrecinctName());
            deviceStatePojoVo.setDeviceType(cfgDevice.getDeviceType());
            deviceStatePojoVo.setDeviceId(cfgDevice.getDeviceId());
            deviceStatePojoVo.setTime(deviceState.getUpdateTime());
            deviceStatePojoVo.setNetState(deviceState.getStatus());
            deviceStatePojoVo.setDeviceName(cfgDevice.getDeviceName());
            deviceStatePojoVo.setPrecinctId(cfgDevice.getPrecinctId());

            deviceStatePojoVoList.add(deviceStatePojoVo);
        }
        //筛选条件
        if (deviceName != null && !deviceName.isEmpty()) {
            List strings = cfgDeviceMapper.queryLikeDeviceName(deviceName);
            deviceStatePojoVoList = deviceStatePojoVoList.stream()
                    .filter((DeviceStatePojoVo dd) -> strings.contains(dd.getDeviceName()))
                    .collect(Collectors.toList());
        }
        //筛选条件集合
        List result = new ArrayList();//
        if (deviceType != 0) {
            result.add(deviceType);//把要筛选的条件添加到集合里
// 要筛选的集合跟筛选条件集合比较 重新赋值给原来的集合 集合对象 条件集合 对比 对象里的属性 deviceStatePojoVoList
= deviceStatePojoVoList.stream().filter((DeviceStatePojoVo dd) -> result.contains(dd.getDeviceType())).collect(Collectors.toList()); } if (precinctId != null && !precinctId.isEmpty()) { result.add(precinctId); deviceStatePojoVoList = deviceStatePojoVoList.stream() .filter((DeviceStatePojoVo dd) -> result.contains(dd.getPrecinctId())) .collect(Collectors.toList()); } if (onLine != null && !onLine.isEmpty()) { result.add(onLine); deviceStatePojoVoList = deviceStatePojoVoList.stream() .filter((DeviceStatePojoVo dd) -> result.contains(dd.getNetState())) .collect(Collectors.toList()); } return deviceStatePojoVoList; }



 

转载于:https://www.cnblogs.com/maocai2018/p/10117117.html

你可能感兴趣的:(List 多条件,多条件筛选集合)