简体   繁体   中英

How to iterate Hashmap of HashMap in Java 8 Stream?

Here is the example:

{
"options": [
{
  "price": {
    "amount": 0
  },
  "serviceOption": {
    "activationDate": "string",
    "conflictsWith": [
      "string"
    ],
    "content": {
      "name": "string",
      "shortText": "string"
    },
    "dependsOn": "string",
    "durationPeriod": "MONTH",
    "earliestDeactivationDate": "string",
    "fee": 0,
    "feePeriod": "MONTH",
    "groupId": "string",
    "installed": true,
    "minimumDuration": 0,
    "modifiable": true,
    "productId": "string",
    "scheduledDeactivationDate": "string",
    "status": "RECURRING"
  }
}
]
}

Here I need to get the value of groupId

options->serviceOption->groupId

As per my requirements, I have above json objects and converted into Hashmap to iterate, How to iterate hashmap of hashmap using stream?

To iterate HashMap i used hashmap.entrySet().stream() but then what should i do to iterate further?

Question updated:

这是迭代可用选项的groupId的方法:

options.stream().map(option -> option.get("serviceOption").get("groupId")).forEach(groupId -> /* Do something with each groupId here */)

One approach, starting from a map, is:

Map<String,Object> map = new HashMap<>(); // Your map initialization code goes here.
Stream<Entry<String, Object>> stream = map.entrySet().stream();
// put code to consume stream here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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