问题
Is there a possible identity representation of Comparator
that could exist?
In the search for simplifying the code in Removing overloaded method in Java, I thought about this and ended up concluding that if every comparison results in objects being equal, the order wouldn't really change making the operation an identity. Hence I ended up with (an inefficient) suggestion such as this:
public static <T, G> List<G> toListOfNewType(List<T> inputList, Function<T, G> mapperFunction) {
return toListOfNewType(inputList, mapperFunction, (a, b) -> 0); // overloaded with comparator for 'G' type
}
But what I now wonder is, would this even hold for objects with their custom compareTo
implementations as well? Is it really safe to assume this given the Stream
implementation?
Edit: Certain tests that I'd tried and which retained the order were as follows:
List<Integer> integers = List.of(1, 3, 45356, 47424, 34234, 4, 4, 234234, 234, 0, -23, -34);
System.out.println(integers);
System.out.println(integers.stream().sorted((a, b) -> 0).collect(Collectors.toList()));
System.out.println(integers.stream().sorted((a, b) -> 0).parallel().collect(Collectors.toList()));
[1, 3, 45356, 47424, 34234, 4, 4, 234234, 234, 0, -23, -34]
[1, 3, 45356, 47424, 34234, 4, 4, 234234, 234, 0, -23, -34]
[1, 3, 45356, 47424, 34234, 4, 4, 234234, 234, 0, -23, -34]
List<String> strings = List.of("aadad", "Z", "vsadasd", "zadad", "C", "Aadasd");
System.out.println(strings);
System.out.println(strings.stream().sorted((a, b) -> 0).collect(Collectors.toList()));
System.out.println(strings.stream().sorted((a, b) -> 0).parallel().collect(Collectors.toList()));
[aadad, Z, vsadasd, zadad, C, Aadasd]
[aadad, Z, vsadasd, zadad, C, Aadasd]
[aadad, Z, vsadasd, zadad, C, Aadasd]
Set<Integer> integerSet = Set.of(1, 3, 45356, 47424, 34234, 4, 234234, 234, 0, -23, -34);
System.out.println(integerSet);
System.out.println(integerSet.stream().sorted((a, b) -> 0).parallel().collect(Collectors.toList()));
System.out.println(integerSet.stream().sorted((a, b) -> 0).collect(Collectors.toList()));
[-34, 45356, 47424, 234, -23, 234234, 1, 34234, 3, 4, 0]
[-34, 45356, 47424, 234, -23, 234234, 1, 34234, 3, 4, 0]
[-34, 45356, 47424, 234, -23, 234234, 1, 34234, 3, 4, 0]
Set<String> stringSet = Set.of("aadad", "Z", "vsadasd", "zadad", "C", "Aadasd");
System.out.println(stringSet);
System.out.println(stringSet.stream().sorted((a, b) -> 0).collect(Collectors.toList()));
System.out.println(stringSet.stream().sorted((a, b) -> 0).parallel().collect(Collectors.toList()));
[zadad, Z, vsadasd, C, Aadasd, aadad]
[zadad, Z, vsadasd, C, Aadasd, aadad]
[zadad, Z, vsadasd, C, Aadasd, aadad]
回答1:
The javadoc of sorted(Comparator) says:
For ordered streams, the sort is stable.
For unordered streams, no stability guarantees are made.
Of course, for an unordered stream, "identity" ordering isn't really a thing anyway, so yeah, the "constant comparator" is an "identity order", in that ordered streams retain their order, and unordered streams remain unordered.
来源:https://stackoverflow.com/questions/58782898/identity-operation-equivalent-for-comparator