0

For the datas below:

List<Object> objs = JSON.parse("[
{\"type\": \"fruit\"},{\"type\": \"fruit\"},
{\"type\": \"fruit\"},{\"type\": \"meat\"},
{\"type\": \"meat\"},{\"type\": \"fruit\"},
{\"type\": \"meat\"},{\"type\": \"fruit\"}
]");

How to use stream to split these datas with type? The following code will work

objs
    .parallelStream()
    .collect(Collectors.groupingby(v -> v.getString("type")))
    .entrySet()
    .stream(entry -> {
       ... // part1
    })

But this means these datas stream will not pass to part1 until collect end. Is there any ways to distribute stream like 'group by'?

  • So no you can't, but if you have a real use case instead of "small JSON of fruits", there may be a good way to solve your actual problem. In the code in your question using `parallelStream()` would not improve performance, since the amount of data is so tiny. – Kayaman Aug 29 '21 at 14:27

0 Answers0