Selecting a specific comparator method when sorting

Christian Mallwitz c.mallwitz at gmail.com
Thu Dec 6 11:30:24 PST 2012


Hi

I have been experimenting with some lambda sorting examples. Examples:

        List<String> list = new ArrayList<>();
        list.add("496");
        list.add("6");
        list.add("8128");
        list.add("28");
        list.add("33550336");

        list
            .stream()

            .sorted(Comparators.comparing((IntFunction<String>)
Integer::parseInt)) // (1)
            .sorted(Comparators.<String>comparing(Integer::parseInt)) // (2)

            .forEach(x -> { System.out.println(x); });

I understand in case (1) syntactically speaking the
(IntFunction<String>) part is just a type cast. What is the name and
specification for the <String> bit in (2)? Could you point me to the
relevant Java spec parts?

Additionally I want to share my attempt at a Schwartz Transformation -
I have two versions: one version (A) requiring an additional class and
one version (B) emitting a warning due to a type cast (which should be
safe). Which one would you prefer? Any other feedback?

Call as in:

        schwartz(list.stream(), Integer::parseInt).forEach(x -> {
System.out.println(x); });

A)

    public static<T, R extends Comparable<? super R>> Stream<T>
schwartz(Stream<T> stream, Function<T, R> f) {

        // class Pair - type of second element of pair must be Comparable
        final class Pair<F, S extends Comparable<? super S>> {
            public final F first;
            public final S second;
            public Pair(F first, S second){ this.first = first;
this.second = second; }
        }

        return stream
            .map(t -> new Pair<>(t, f.apply(t)))
            .sorted((p1, p2) -> p1.second.compareTo(p2.second))
            .map(p -> p.first);
    }

B)

    @SuppressWarnings("unchecked")
    public static<T, R extends Comparable<? super R>> Stream<T>
schwartz(Stream<T> stream, Function<T, R> f) {

        return stream
            .map(t -> new Object[]{t, f.apply(t)})
            .sorted((o1, o2) -> ((Comparable) o1[1]).compareTo(o2[1]))
            .map(o -> ((T) o[0]));
    }

Cheers
Christian


More information about the lambda-dev mailing list