Reputation: 1594
I want to optimizes ObjectMapper for a list. The requirement is that I need to add a delimiter after each element of the list. My current code looks like :
StringBuilder strBuilder = new StringBuilder();
for (Event event : segregatedList) {
String eventJson = mapper.writeValueAsString(event);
strBuilder.append("\n");
strBuilder.append(eventJson);
}
This take a huge amount of time for a long list (~10000 events) .How can I optimize the code to do serializes the list in a single go ?
Upvotes: 2
Views: 637
Reputation: 4591
mapper instances are thread-safe, so you can split the mapper.writeValueAsString to a parallel job. I guess something like this may help if you don't worry of the order in which they are appended!
segregatedList.parallelStream().map(event -> mapper.writeValueAsString(event)).collect(Collectors.joining("\n")))
Otherwise, I can see very minimum scope of improving here. Maybe you can optimize json by ignoring properties as mentioned by Dark Knight
Upvotes: 2
Reputation: 8337
There are multiple ways to concatenate Strings in java.
From my personal analysis i can say +
call on String gets translated into new StringBuilder().append( "" )
. Since StringBuilder(String) constructor allocates a buffer with 16 char, appending more than 16 characters will require buffer reallocation. At last, StringBuffer.toString() calls create a new String object with a copy of StringBuilder buffer.
So if you don't want synchronization overhead StringBuilder
stands best among others else i would advice you to use StringBuffer
here. I see you are using StringBuilder
already, so there is very little scope for improvement here. However you can optimize generated json by ignoring properties which are not useful.
Upvotes: 0