简体   繁体   English

在数组 Object 的情况下如何读取特定的 Map 键值

[英]How to read Specific Map Key Value in case of an Array Object

I have the following Map structure我有以下 Map 结构

{empId=1234, empName=Mike, CDetails=[{"collegeName":"Peters Stanford","collegeLoc":"UK","collegeLoc":"UK"}]}

I need to read the value collegeLoc from the above Map我需要从上面的 Map 中读取值collegeLoc

I tried this way, its working, but is there any better way我试过这种方式,它的工作,但有没有更好的方法

    myMap.entrySet().stream().filter(map -> map.getKey().equals("CDetails")).forEach(e -> {

            List<Object> objsList = (List<Object>) e.getValue();

            for(int i=0;i<objsList.size();i++)
            {
                HashMap<String,String> ltr = (HashMap<String, String>) objsList.get(i);

                System.out.println(ltr.get("collegeLoc"));
            }



        });

CDetails is a List , not a Map . CDetailsList ,而不是Map

Try this:尝试这个:

empMap.entrySet().stream()
  .map(map -> map.get("CDetails"))
  .filter(Objects::nonNull)
  .flatMap(List::stream)
  .map(element -> ((Map)element).get("collegeLoc"))
  .filter(Objects::nonNull)
  .forEach(System.out::println);

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

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