ObjectNode row = Json.newObject();
row.put("0", a);
row.put("1", x);
row.put("2", y);
now I have list
List<String> list = new ArrayList<String>();
How can I add this to the row?
ObjectNode row = Json.newObject();
row.put("0", a);
row.put("1", x);
row.put("2", y);
now I have list
List<String> list = new ArrayList<String>();
How can I add this to the row?
You can use the putArray method which creates an ArrayNode. Then you should fill it with the elements from your list.
ArrayNode arrayNode = row.putArray("myList");
for (String item : list) {
arrayNode.add(item);
}
addAll accepts a Collection. However, only collection of JsonNodes. I'll update my answer - thank you!Here is a functional way of creating an arrayNode and populate it elements from a stream. It does the same as above fracz answer :
yourStringList.stream()
.collect(Collector.of(
() -> row.putArray("myList"),
(arrayNode, item) -> arrayNode.add(item),
(arrayNode1, arrayNode2) -> { arrayNode1.addAll(arrayNode2); return arrayNode1; },
Collector.Characteristics.IDENTITY_FINISH
));
Essentially, it defines a custom collector to accumulate the elements provided by the stream. Some explanations:
() -> row.putArray("myList") defines the function which will provide the object to accumulate the items (called the supplier function). In our case this function will provide the ArrayNode and is the equivalent of ArrayNode arrayNode = row.putArray("myList"); statement in the above answer.(arrayNode, item) -> arrayNode.add(item) defines the accumulator function whose role is to add the elements. In the left side of the -> operator, the first variable (e.g. arrayNode) refers to the container defined in the first argument while the second (e.g. item) refers to the element to accumulate provided by the stream. This function does the same job as the statement arrayNode.add(item); inside the loop in the above example.(arrayNode1, arrayNode2) -> { arrayNode1.addAll(arrayNode2); return arrayNode1; } is the combiner function which is used if the stream is collected in parralel (for instance if we had .parralelStream() instead of .stream()). Its role is to merge two accumulators toghether and this is why the function has two argumets of the same type. We define this function such that it will add all elements accumulated in the second accumulator into the first accumulator and return this first accumulator.Collector.Characteristics.IDENTITY_FINISH define one or more optons to the process. In this case, IDENTITY_FINISH simply states that no finisher function will be provided (to further process the accumulator object) or, in other words, that the finisher function is the identity function (no treatment done).This solution might seems more complex at first but it leverages all the power of functionnal programming. For instance, we could stream objects, extract a String property with the map function, filter it, etc. before collecting everything:
yourObjectList.stream()
.map(o -> o.getTheStringProperty())
.filter(o -> o.length() > MIN_SIZE)
.collect(Collector.of(
() -> row.putArray("myList"),
(arrayNode, item) -> arrayNode.add(item),
(arrayNode1, arrayNode2) -> { arrayNode1.addAll(arrayNode2); return arrayNode1; },
Collector.Characteristics.IDENTITY_FINISH
));