Selecting a specific comparator method when sorting
Marc Petit-Huguenin
marc at petit-huguenin.org
Thu Dec 6 13:03:08 PST 2012
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
On 12/06/2012 11:30 AM, Christian Mallwitz wrote:
> 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])); }
>
If we still had MapStream:
public static<T, R extends Comparable<? super R>> Stream<T> schwartz(Stream<T>
stream, Mapper<T, R> f) {
return stream.mapped(f)
.swap()
.sorted((p1, p2) -> p1.compareTo(p2))
.values();
}
- --
Marc Petit-Huguenin
Email: marc at petit-huguenin.org
Blog: http://blog.marc.petit-huguenin.org
Profile: http://www.linkedin.com/in/petithug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
iQIcBAEBCAAGBQJQwQgKAAoJECnERZXWan7EcTEQAJlBXNmKe/iin0LMoDWQE8ct
2E0GIfD2hVBNelXsUjipiC6pm9TNjI1pCxlz+1CqnhNZuLt/o3hm+FvoVbfQa7SP
4/EBL2gcY290+fWBbPbmJ69NwCZDGrLyEMfl9Ac+DrPzKyaN+d1ZzqtOF+FFL8+a
6IZcd2AjLvtDTpVs7yckJt7NuJ9YEn4cLWP1KEwz0+bGI1EoolP8v3GBCdnXfVfM
X6isMNQ9lZEzyHD5/vmjjVdWfT5PdIxasmVe5pn4R5wu0EpunBvaDMzBEQGdwyVJ
c/pPs+aWgj1rtd+8oO4lg/XNxNksRnAIvuWVH75sdYLJtRNaxoigDuqHf6JKzrtS
DUg/RJAqRqNANAxJFcf/D+VWZW0+tmH3trL399BqiF1fiF2GxerJ8UmHAe7R1OnD
2ytsvyA7nY9S21vO3dDB7eKlvhhRqgmT2OshhgigRfhMlrTlYX1Nj349K2Dt4t7b
SjDx7G8VkFRP1YxRlJhfxURr+96/0Zpse6nWboBhhqATPrBEzzevCj+FKJ/+s6fQ
E6b8GZ1VRrDwuii252v+XBMhOSkGpAJ8ujGAg+Xa6kGYtTsOSPU8aqyIUO+YSVTL
RF8Iy+DRG6FZnI2i8dTQm1OCIJqeSlENEHH3RkTRsds+y55mfOBnhsJeiSJyFipM
Ej4UcMVswcA0tAUZyddA
=bh0J
-----END PGP SIGNATURE-----
More information about the lambda-dev
mailing list