Java:Compareable>>

发布于 2024-09-28 04:49:18 字数 270 浏览 0 评论 0 原文

Java 中是否有 Compareable>> 实现(其行为类似于 C++ 的 std::list::operator<() 或 std::set::operator<())?


编辑: Comparator 会更有意义......

Is there any Compareable<Collection<T extends Compareable<T>>> implementation in Java (which behaves as C++'s std::list<T>::operator<() or std::set<T>::operator<())?


Edit: Comparator would make more sense...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生死何惧 2024-10-05 04:49:18

据我所知,但写起来应该不会太难。

compareTo(Collection<T> other) {
    Iterator<T> i1 = this.iterator();
    Iterator<T> i2 = other.iterator();
    while(i1.hasNext() && i2.hasNext()) {
        int c = i1.next().compareTo(i2.next());
        if(c != 0) {
            return c;
        }
    }
    if(i1.hasNext()){
        return 1;
    } else if(i2.hasNext()) {
        return -1;
    } else {
        return 0;
    }
}

Not that I am aware of, but it shouldn't be too difficult to write.

compareTo(Collection<T> other) {
    Iterator<T> i1 = this.iterator();
    Iterator<T> i2 = other.iterator();
    while(i1.hasNext() && i2.hasNext()) {
        int c = i1.next().compareTo(i2.next());
        if(c != 0) {
            return c;
        }
    }
    if(i1.hasNext()){
        return 1;
    } else if(i2.hasNext()) {
        return -1;
    } else {
        return 0;
    }
}
月下凄凉 2024-10-05 04:49:18

我不知道你提到的那些 C++ 运算符,但我假设你想要的是一个按字典顺序比较集合的比较器。

Guava 通过其出色的 Ordering 类实现了这一点:Ordering.lexicographyal()

返回一个新的排序,它通过成对比较相应的元素直到找到非零结果来对可迭代对象进行排序;强加“字典顺序”。如果到达一个迭代的末尾,但未到达另一个迭代的末尾,则较短的迭代被认为小于较长的迭代。例如,整数的字典自然排序考虑 [] < [1] < [1, 1] < [1, 2] < [2].

假设您想根据 String 的自然顺序对 List> 进行排序:

List<List<String>> lists = ...; 
Ordering<Iterable<String>> comparator = Ordering.natural().lexicographical();
Collections.sort(lists, comparator);

鉴于这是 Ordering 的一部分> 类,您也可以获得它的全部功能,包括将它与任何任意比较器一起使用的能力:

/*
 * This comparator will use a case insensitive comparison of individual
 * strings in determining the ordering.
 */
Ordering<Iterable<String>> comparator =
    Ordering.from(String.CASE_INSENSITIVE_ORDER).lexicographical();

/*
 * This comparator uses a Function<Foo, Long> (Foo.GET_ID) to compare the IDs
 * of Foo instances.
 */
Ordering<Iterable<Foo>> comparator = 
     Ordering.natural().onResultOf(Foo.GET_ID).lexicographical();

I don't know about those C++ operators you mention, but I'm assuming what you want is a comparator that compares the collections lexicographically.

Guava has this through its excellent Ordering class: Ordering.lexicographical()

Returns a new ordering which sorts iterables by comparing corresponding elements pairwise until a nonzero result is found; imposes "dictionary order". If the end of one iterable is reached, but not the other, the shorter iterable is considered to be less than the longer one. For example, a lexicographical natural ordering over integers considers [] < [1] < [1, 1] < [1, 2] < [2].

Say you wanted to order a List<List<String>> based on String's natural order:

List<List<String>> lists = ...; 
Ordering<Iterable<String>> comparator = Ordering.natural().lexicographical();
Collections.sort(lists, comparator);

Given that this is part of the Ordering class, you get its full power as well, including the ability to use it with any arbitrary comparator:

/*
 * This comparator will use a case insensitive comparison of individual
 * strings in determining the ordering.
 */
Ordering<Iterable<String>> comparator =
    Ordering.from(String.CASE_INSENSITIVE_ORDER).lexicographical();

/*
 * This comparator uses a Function<Foo, Long> (Foo.GET_ID) to compare the IDs
 * of Foo instances.
 */
Ordering<Iterable<Foo>> comparator = 
     Ordering.natural().onResultOf(Foo.GET_ID).lexicographical();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文