Java examples for Lambda Stream:Lambda
The following code shows how to get To String Function
import java.lang.reflect.Array; import java.util.function.Function; public class Main{ public static void main(String[] argv){ System.out.println(getToStringFunction()); }//ww w . ja va 2 s . com /** * A function which returns the {@code String} representation of an object. * For arrays, a readable {@code String} representation is returned. */ private static final Function<?, String> TO_STRING_FUNCTION = o -> { return o.getClass().isArray() ? toString(o) : o.toString(); }; /** * @return a function that creates readable {@code String} representations of arrays. */ static <T> Function<T, String> getToStringFunction() { @SuppressWarnings("unchecked") Function<T, String> func = (Function<T, String>) TO_STRING_FUNCTION; return func; } private static String toString(Object array) { int length = Array.getLength(array); if (length == 0) { return "[]"; } StringBuilder b = new StringBuilder(); b.append('['); for (int i = 0;; i++) { b.append(Array.get(array, i)); if (i == length - 1) { return b.append(']').toString(); } b.append(", "); } } }