Loop on ArrayNode tranform to stream

Loop on ArrayNode tranform to stream



I'm trying to use streams() to convert this code. I'm using jackson library.


JsonNode convert(final ObjectNode src)
boolean checkNotEmpty(final JsonNode node)


ArrayNode newArray = mapper.createArrayNode();
for (final JsonNode node: arrayNodes)
JsonNode newNode = convert((ObjectNode) fiwareNode);
if (checkNotEmpty(newNode))
newArray.add(newNode);




Currently I have:


ArrayNode a = StreamSupport.stream(nodes.spliterator(), true)
.map(e -> convert((ObjectNode)e))
.filter(e -> checkNotEmpty(e))
.collect(Collector.of(
mapper.createArrayNode(), // init accumulator
ArrayNode::add, // processing each element
));



However I'm getting:


The type ArrayNode does not defined add(R,T) that is applicable here



Any idea on how to fix it?



Edit:



I found this. Do I have to implement this class? Is it possible to do it inline? So I guess this is going to be the solution ;)



This question came from our site for system and network administrators.





Your code is not complete. Collector.of accepts at least three elements.
– MC Emperor
Aug 28 at 7:03


Collector.of





Thanks.. I realized that I didn't post the full code :( I think the answer is the link I included.
– jlanza
Aug 28 at 18:42




2 Answers
2



Something like this?


ArrayNode newArray = mapper.createArrayNode();
arrayNodes.stream()
.map(YourClass::convert)
.filter(YourClass::checkNotEmpty)
.forEach(newArray::add);



I know it's not a one-liner, but why bother writing a custom collector?



You can do it like this (ArrayNode has a addAll iirc)


ArrayNode


addAll


arrayNodes.stream()
.map(YourClass::convert)
.filter(YourClass::checkNotEmpty)
.forEach(newArray::add);
.collect( mapper.createArrayNode(),
(l, e) -> l.add(e),
(l1, l2) -> l1.addAll(l2));





This was my first option, but I wanted to know how to use Collectors ;) Please see my edit on question.
– jlanza
Aug 28 at 7:02





You can construct a Collector inline, but it is not very readable imho. Your linked article creates a nice custom collector, which I would use here :)
– RobAu
Aug 28 at 7:15



You could of course create a new Collector. The one in the link you found just does what you want it to do. This is especially useful when you need this piece multiple times.


Collector



But if you want to just write a single statement — one of the overloaded versions of the collect method in the Stream class accepts a supplier, an accumulator and a combiner. The ObjectMapper class can create an ArrayNode instance through the createArrayNode method, while the ArrayNode class provides add and addAll methods. These are all you need:


collect


Stream


ObjectMapper


ArrayNode


createArrayNode


ArrayNode


add


addAll


ArrayNode newArray = arrayNodes.stream()
.map(t -> convert((ObjectNode) t))
.filter(YourClass::checkNotEmpty)
.collect(mapper::createArrayNode, ArrayNode::add, ArrayNode::addAll);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)