0

Basically I have a List<Map<String, Object>> listOfValueand I need to check if the object is instance of byte then encode it to String as shown below:

private void convertByteToBase64(List<Map<String, Object>> listOfValue) {

Object value = null;


if (!CollectionUtils.isEmpty(listOfValue)) {

    for (Map<String, Object> map : listOfValue) {

        if (!map.isEmpty()) {

            for (Map.Entry<String, Object> entry : map.entrySet()) {

                value = entry.getValue();

                if (value instanceof byte[]) {

                    entry.setValue(Base64.getEncoder().encodeToString((byte[]) value));
                }
            }

        }

    }

}

}

I am using java 8 and it is working as expected but is it the correct way of doing it or any better way in term of performance?

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
user1999453
  • 1,297
  • 4
  • 29
  • 65

3 Answers3

2

Current implementation seems to be ok, however, checking for emptiness of the list and the nested maps seems to be redundant.

Some performance improvement may be possibly achieved if parallel streams are used to iterate the list/maps.

private void convertByteToBase64(List<Map<String, Object>> listOfValue) {
    Base64.Encoder base64encoder = Base64.getEncoder();
    listOfValue.parallelStream()
        .flatMap(map -> map.entrySet().parallelStream())
        .filter(entry -> entry.getValue() instanceof byte[])
        .forEach(entry -> entry.setValue(
            base64encoder.encodeToString((byte[]) entry.getValue())
        ));
}

Base64.Encoder is thread-safe: Instances of Base64.Encoder class are safe for use by multiple concurrent threads..

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • 3
    null != entry.getValue() && entry.getValue() instanceof byte[], first condition is seem redundant also, entry.getValue() instanceof byte[] is enough – Huy Nguyen Dec 22 '21 at 09:55
  • Right, thanks for noticing, I edited the [redundant null-check](https://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof) out – Nowhere Man Dec 22 '21 at 10:12
  • @AlexRudenko Thnks for your answer, a quick question why is checking for emptiness of the list and the nested maps redundant? – user1999453 Dec 22 '21 at 11:27
  • 1
    @user1999453, explained here: [Is calling isEmpty() before iterating redundant?](https://stackoverflow.com/questions/43852518/is-calling-isempty-before-iterating-redundant) – Nowhere Man Dec 22 '21 at 11:38
0
public static void convertByteToBase64(List<Map<String, Object>> listOfValue) {

    listOfValue.stream().parallel()
            .forEach(map -> map.forEach((key,value)->{
                if(value instanceof byte[]) {
                    map.put(key, Base64.getEncoder().encodeToString((byte[])value))  ;
        }
    }));
}
Oussama ZAGHDOUD
  • 1,767
  • 5
  • 15
0

Alternatively, you can use parallelStream and filter as below.

private void convertByteToBase64(List<Map<String, Object>> listOfValue) {
           listOfValue.parallelStream().forEach(map -> {
            map.entrySet().parallelStream().filter(entry->entry.getValue() instanceof byte[]).forEach(entry -> {
                entry.setValue(Base64.getEncoder().encodeToString((byte[]) entry.getValue()));
            });
          });`
}