等同于比较器的身份操作 [英] Identity operation equivalent for Comparator

查看:75
本文介绍了等同于比较器的身份操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在可能存在的比较器身份表示?

Is there a possible identity representation of Comparator that could exist?

在寻找简化代码的过程中在删除Java中的重载方法中,我想到了这一点,最终结论是,如果每次比较都导致对象相等,则顺序不会真正改变,从而使操作成为 identity 。因此,我最终得到了这样一个(效率低下的)建议:

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
}

但我现在想知道的是,这对于具有自定义 compareTo 实现的对象也适用吗?在给定$ Stream 实现的情况下,真的可以肯定吗?

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]


推荐答案

sorted(Comparator) 说:


对于有序流,排序是稳定

对于无序流,没有稳定性保证

当然,对于无序流,身份是指无论如何,排序并不是一件真正的事情,是的,常量比较器是身份顺序,因为有序流保持其顺序,而无序流保持无序。

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.

这篇关于等同于比较器的身份操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆