天天看點

java linq過濾及指派二三例

1、從集合中過濾出符合條件的元素(例子1)

//從集合List<>中獲得符合條件的第一個元素
//例子中,ybService.getWaves() 傳回 List<Yb_Wave>
Yb_Wave wave = ybService.getWaves().stream()
        .filter(w -> name.equals(w.getName()))
        .findAny()
        .orElse(null);      

2、從集合中過濾出符合條件的元素(例子2)

//lis.get(0).getChildren()本身是一個集合
List<MapLayer> lis = Utils.getMapLayers("public");
Arrays.stream(lis.get(0).getChildren())
        .filter(w -> subject.equals(w.getTitle()))
        .findAny()
        .orElse(null)      

3、從集合中過濾出符合條件的元素,然後修改

String imgPath = "/base/images/";

List<WorkBAquatic> lis = this.workBAquaticDao.queryAllByLimit(offset, limit);
lis.stream()
        .filter(w -> w.getImg() != null)
        .forEach(w -> {
            w.setImg(imgPath + w.getImg());
        });
return lis;      

2021.05.18

JSONArray devices = ...

devices.stream()
  .map(o -> (JSONObject) o)//多了一步對象轉換?
  .filter(jo -> jo.get("regionIndexCode").toString().compareTo(regionCode) == 0)
  .forEach(jo -> {
    CameraDevice device = new CameraDevice(jo.get("indexCode").toString(),
        jo.get("name").toString(),
        jo.get("regionIndexCode").toString(),
        Integer.parseInt(jo.get("cameraType").toString()));

    lis.add(device);
  });