简体   繁体   English

使用java从数组中过滤id值

[英]Filtering id value from Array using java

I am trying to filter the id value which matches a certain variable and return the id我正在尝试过滤与某个变量匹配的 id 值并返回 id

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

folderName = Dev2020-12文件夹名称 = Dev2020-12

code:代码:

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

I need to match the value in the result and return only the id of the matched drive name.我需要匹配结果中的值并只返回匹配驱动器名称的 id。

expected output:预期输出:

"0AAPUk9PVA" “0AAPUk9PVA”

How can i achieve this我怎样才能做到这一点

As soon as result appears to be com.google.api.services.drive.model.DriveList , you can try the following:一旦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();

result.stream().filter(i -> i.get("name").equals(foderName)).findAny().orElse(null)

过滤器调用中的检查将取决于您在 ArrayList 中的对象类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM