Java:使用 Collat​​orKey 对集合进行排序

发布于 2024-08-04 07:16:39 字数 874 浏览 2 评论 0原文

我想要实现的是按字符串值对对象集合进行排序。但是,以依赖于语言环境的方式使用整理器。由于性能原因,我不想使用 Collat​​orcompare() 方法(如下代码所示),而是使用 Collat​​ionKey 类,因为 java API 声明使用 Collat​​ionKey 要快得多。

但是如何使用 Collat​​ionKey 实现compareTo() 方法呢?据我了解,如果我要使用 Collat​​ionKey,我必须自己完全编写所有比较方法。因此,我什至将不再能够使用 Collections.sort() 方法...我非常感谢一个易于理解的示例,以及使用 Collat​​ionKey 对 Person 对象集合进行排序的最有效的实现。

谢谢你!

public class Person implements Comparable<Person> {

String lastname;

public int compareTo(Person person) {
     //This works but it is not the best implementation for a good performance
     Collator instance = Collator.getInstance(Locale.ITALY);
     return instance.compare(lastname, person.lastname);
}
}

...
ArrayList list = new ArrayList();
Person person1 = new Person("foo");
list.add(person1);
Person person2 = new Person("bar");
list.add(person2);
Collections.sort(list);
...

what I would like to achieve is to sort a colletion of objects by a string value. However in a locale dependant way using a collator. Due to performance reasons I do not want to use the Collator compare() method (as below in the code) rather the CollationKey class, as the java API states the using a CollationKey is much faster.

But how do I implement the compareTo() method using the CollationKey? As far as I understood it, I have to completely write all the comparison Methods on my own if I will be using a CollationKey. So I will even no longer be able to use the Collections.sort() methods... I am very thankfull for an example that is easy to understand and a the most efficient implementation to sort the Collection of Person objects using a CollationKey.

Thank you!

public class Person implements Comparable<Person> {

String lastname;

public int compareTo(Person person) {
     //This works but it is not the best implementation for a good performance
     Collator instance = Collator.getInstance(Locale.ITALY);
     return instance.compare(lastname, person.lastname);
}
}

...
ArrayList list = new ArrayList();
Person person1 = new Person("foo");
list.add(person1);
Person person2 = new Person("bar");
list.add(person2);
Collections.sort(list);
...

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

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

发布评论

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

评论(3

夏有森光若流苏 2024-08-11 07:16:39
class Person implements Comparable<Person> {

  private static final Collator collator = Collator.getInstance(Locale.ITALY);

  private final String lastname;

  private final CollationKey key;

  Person(String lastname) {
    this.lastname = lastname;
    this.key = collator.getCollationKey(lastname);
  }

  public int compareTo(Person person) {
     return key.compareTo(person.key);
  }

}
class Person implements Comparable<Person> {

  private static final Collator collator = Collator.getInstance(Locale.ITALY);

  private final String lastname;

  private final CollationKey key;

  Person(String lastname) {
    this.lastname = lastname;
    this.key = collator.getCollationKey(lastname);
  }

  public int compareTo(Person person) {
     return key.compareTo(person.key);
  }

}
秋日私语 2024-08-11 07:16:39
  1. 创建一个 SortedMap m,其中 T 是要使用 Collat​​ionKeys 排序的对象的类型。您可以使用 TreeMap 作为实现
  2. 对于要排序的每个 e 元素, m.put(collat​​or.getCollat​​ionKey(e.{getStringYouWantToSortOn}), e);

迭代m.values() 应该生成您的对象,并使用 Collat​​ionKeys 按您想要的字符串排序。

我相信这效率不高,但应该有效。

  1. Create a SortedMap m, where T is the type of the objects you want to sort using CollationKeys. You can use TreeMap as the implementation
  2. For each e element you want to sort, m.put(collator.getCollationKey(e.{getStringYouWantToSortOn}), e);

Iterating over m.values() should yield your objects, sorted by the string you want using CollationKeys.

I believe this is not efficient, but it should work.

半衬遮猫 2024-08-11 07:16:39

使用比较器而不是使人具有可比性。您的 Comparator 可以采用 2 个 Persion 实例,并根据某个 Collat​​or 实例对它们进行比较。然后打电话

Collections.sort(list, myPersonComparator);

use a Comparator instead of making Person Comparable. your Comparator can take 2 Persion instances and compare them based on some Collator instance. then call

Collections.sort(list, myPersonComparator);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文