繁体   English   中英

转换列表<list<object> &gt; 至 Map<string,string> Java 8 </string,string></list<object>

[英]Convert List<List<Object>> to Map<String,String> Java 8

我有一个列表,其中 OcrImageLocation 包含列表和字符串 s3_id;

我只是想使用 java 8 将列表转换为 map ,其中包含 s3_id 作为键和 image_location 作为值

public class PageLocationInfo {
   @JsonProperty("page_index")
   private String page_index;
   @JsonProperty("location")
   private String location;
   @JsonProperty("image_location")
   private String image_location;
}
public class OcrImageLocation {
   private List<PageLocationInfo> page_info;
   @JsonProperty("s3_id")
   private String s3_id;
}

你可以将 map 的每一对OcrImageLocationPageLocationInfo到一个Map.Entry<String,String>对应的 s3_id + page_index 和 image_location 中:

Map<String, String> map =
    input.stream()
         .flatMap(oil -> oil.getPageInfo()
                            .stream()
                            .map(pli -> new SimpleEntry<>(oil.getS3Id() + pli.getPageIndex(),
                                                          pli.getImageLocation())))
         .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));

您可以使用 Stream 如下所示,

Map<String, String> result = locationList.stream().map(e -> e.getPage_info().stream()
          .collect(Collectors.toMap(e1 -> e.getS3_id() + e1.getPage_index(),
                  PageLocationInfo::getImage_location)))
          .flatMap(e -> e.entrySet().stream())
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

暂无
暂无

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

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