14
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?

1
  • What are a x and y..Are they int, string etc Commented Dec 12, 2015 at 13:28

2 Answers 2

26

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

addAll expects an ArrayNode or a liste of JsonObjects, you'll want to iterate through your list to add its strings to the new ArrayNode.
@EricMaziade, you are right. I have mistakenly spotted that the addAll accepts a Collection. However, only collection of JsonNodes. I'll update my answer - thank you!
0

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:

  • the first argument () -> 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.
  • the second argument (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.
  • the third argument (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.
  • the fourth argument 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
        ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.