1:java8特性 .stream()的使用
public void updateMySceneEngineBeanList() {
Long userId = DataMMKV.getUserId();
List<SceneEngineBean> sceneEngineBeans = SceneEngineDepository
.getInstance()
.getSceneEngineListData(userId);
List<SceneEngineBean> localSceneEngineBeans = SceneEngineDepository
.getInstance()
.getLocalSceneEngineStateListData(userId)
.stream()
.map(LocalSceneEngineBean::getSceneEngineBean)
.collect(Collectors.toList());
sceneEngineBeans.addAll(localSceneEngineBeans);
List<ItemBean<SceneEngineBean>> beans = sceneEngineBeans
.stream()
.map(sceneEngineBean -> {
ItemBean<SceneEngineBean> itemBean = new ItemBean<>();
itemBean.setCode(String.valueOf(sceneEngineBean.getId()));
itemBean.setName(sceneEngineBean.getName());
itemBean.setState(SimpleSceneEngineExecuteService.getInstance().isOpen(sceneEngineBean.getId()));
itemBean.setData(sceneEngineBean);
Log.d(TAG, "map: size: " + GsonUtils.toJson(sceneEngineBean));
return itemBean;
}).collect(Collectors.toList());
}
public SceneEngineBean getSceneEngineBean(){
SceneEngineBean sceneEngineBean = new SceneEngineBean();
sceneEngineBean.setId(this.getId());
sceneEngineBean.setName(this.getName());
sceneEngineBean.setRuleJson(this.getRuleJson());
sceneEngineBean.setType(this.getType());
sceneEngineBean.setUserId(this.getUserId());
sceneEngineBean.setSceneEngineRule(this.getSceneEngineRule());
sceneEngineBean.setKeyType(KEY_TYPE_LOCAL);
sceneEngineBean.setKeyValue(this.getKeyId());
return sceneEngineBean;
}
.stream()的意思是把List转化成为流
.map的意思是转化
LocalSceneEngineBean::getSceneEngineBean的意思是通过LocalSceneEngineBean里的getSceneEngineBean方法,把LocalSceneEngineBean转化成SceneEngineBean。
当然了用下面的x->{}也可以实现
网友评论