Is there a way to simplify this stream expression?

protected static String paramsAsString(Object[] args) {
    return Arrays.stream(args).map((o) -> o != null && o.getClass().isArray() ? ArrayUtils.toString(o) : o)
            .collect(Collectors.toList()).toString();
}

unit test

public void paramsAsString() throws Exception {
    String[] strings = {"1" , "2"};
    int[] ints = {3,4};
    int[] intsEmpty = {3,4};
    Object[] args = {"aaa" ,"zzz" , ints , strings, intsEmpty, null};
    String paramsAsString = paramsAsString(args);
    assertEquals("[aaa, zzz, {3,4}, {1,2}, {3,4}, null]", paramsAsString);
}

I am just learning the api stream. I wonder if there is a way to simplify this stream expression and remove the complex if?

+4
source share
2 answers

If you just refer to ArrayUtils::toString, you avoid the conditional:

protected static String paramsAsString(Object[] args) {
    return Arrays.stream(args)
                 .map(ArrayUtils::toString)
                 .collect(Collectors.toList()).toString();
}

but your test fails because you get:

[aaa, zzz, {3,4}, {1,2}, {3,4}, {}]

instead

[aaa, zzz, {3,4}, {1,2}, {3,4}, null]

You can hack this with a regex:

 return Arrays.stream(args)
              .map(ArrayUtils::toString)
              .collect(Collectors.toList())
              .toString().replaceAll("\\{\\}" , "null");
 }

But personally, I would save the conditional expression and convert the lambda to another utility method:

public class MyArrayUtils {
     protected static String paramsAsString(Object[] args) {
         return Arrays.stream(args)
                      .map(MyArrayUtils::objectToString)
                      .collect(Collectors.toList()).toString();
     }

     private static String objectToString(Object object) {
         return object == null ? null : ArrayUtils.toString(object);
     }
}
+5
source

, , , , , (.. ArrayUtils.toString(o) List.toString).

Arrays.deepToString,

"[aaa, zzz, [3, 4], [1, 2], [3, 4], null]"

API Stream , :

, java.lang.reflect.Array

public class MyArrayUtil {
    static String paramsAsString(Object o) {
        if(o==null) return "null";
        if(!o.getClass().isArray()) return o.toString();
        return IntStream.range(0, Array.getLength(o))
            .mapToObj(i->Array.get(o, i)).map(MyArrayUtil::paramsAsString)
            .collect(Collectors.joining(", ", "{", "}"));
    }
}

, . List toString(). Collector, . {, }, :

{aaa, zzz, {3, 4}, {1, 2}, {3, 4}, null}

, :

static String paramsAsString(Object o) {
    if(o==null) return "null";
    if(!o.getClass().isArray()) return o.toString();
    Stream<String> stream;
    if (o instanceof Object[]) {
        stream=Arrays.stream((Object[]) o).map(MyArrayUtil::paramsAsString);
    }
    else if (o instanceof int[]) {
        stream=Arrays.stream((int[])o).mapToObj(String::valueOf);
    }
    else if (o instanceof long[]) {
        stream=Arrays.stream((long[])o).mapToObj(String::valueOf);
    }
    else if (o instanceof double[]) {
        stream=Arrays.stream((double[])o).mapToObj(String::valueOf);
    }
    else stream=IntStream.range(0, Array.getLength(o))
        .mapToObj(i->Array.get(o, i)).map(Object::toString);
    return stream.collect(Collectors.joining(", ", "{", "}"));
}
+4

All Articles