用java从数组中过滤id值

enyaitl3  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(287)

我试图过滤与某个变量匹配的id值并返回id

result = {
    "drives": [{
        "id": "0AEz3mOk9PVA",
        "name": "Dev2020-10"
    }, {
        "id": "0AMEH9PVA",
        "name": "Dev2020-11"
    }, {
        "id": "0AAPUk9PVA",
        "name": "Dev2020-12"
    }
]

文件夹名称=dev2020-12
代码:

if(result.toString().contains(folderName)) {                        
    return result; 
} else {
    // ...
}

我需要匹配结果中的值并只返回匹配驱动器名的id。
预期产量:
“0aapuk9pva”
我怎样才能做到这一点

8dtrkrch

8dtrkrch1#

一旦 result 似乎是 com.google.api.services.drive.model.DriveList ,您可以尝试以下操作:

import com.google.api.services.drive.model.Drive;

Optional<String> folderNameDriveId = result.getDrives().stream()
    .filter(drive -> folderName.equals(drive.getName()))
    .map(Drive::getId)
    .findFirst();
8iwquhpp

8iwquhpp2#

result.stream().filter(i -> i.get("name").equals(foderName)).findAny().orElse(null) filter调用中的检查将取决于arraylist中对象的类型

相关问题