1

I'm trying to convert a List of JsonNodes to a list of fieldNames.

This is what I've cobbled together:

final ResponseBody<JsonNode> body = state.getResponse().getBody();
final List<JsonNode> actualEntryList = body.jsonPath().getList('path.to.something');

final List<String> actualKeyList = actualEntryList.stream()
    .flatMap(node ->                                   // flatmap needs to return a stream
        StreamSupport.stream(node.fieldNames(), false) // fieldNames return an iterator
    ).collect(Collectors.toList());

But I get the error Cannot infer type argument(s) for <R> flatMap(Function<? super T, ? extends Stream<? extends R>>), which makes me think I did it wrong?

How should I be trying to make this conversion?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
  • 2
    [`StreamSupport.stream(…)` expects a `Spliterator`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/StreamSupport.html#stream-java.util.Spliterator-boolean-), not an `Iterator`. You need to [convert the `Iterator` to a `Spliterator`](https://docs.oracle.com/javase/8/docs/api/java/util/Spliterators.html#spliteratorUnknownSize-java.util.Iterator-int-). – Holger Jun 29 '20 at 17:03
  • 1
    [How to convert an iterator to a stream?](https://stackoverflow.com/q/24511052/2711488) – Holger Jun 29 '20 at 17:06

0 Answers0