Java:使用 TreeSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7776203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:22:50  来源:igfitidea点击:

Java: using TreeSet

java

提问by Belgi

I want to go over the objects inside my TreeSet, I can do it by making an array using toArraybut I don't need to go over all of the objects in the Set.

我想查看我的 中的对象TreeSet,我可以通过使用创建一个数组来完成,toArray但我不需要查看 Set 中的所有对象。

How can I go over the objects in the Set (starting with the first than the second etc') ?

如何查看 Set 中的对象(从第一个开始而不是第二个等)?

And another small question about TreeSet: Can I keep the objects in the TreeSetsorted (so the first object will be with the smallest key etc') ?

另一个小问题是TreeSet:我可以将对象保留在TreeSet排序中(因此第一个对象将具有最小的键等)?

edit: say I have a class myInt (with int myInteger) and I want to use it in TreeSet with a different oredering than the natural one, what do I need to define in my class (myInt) to do this ?

编辑:假设我有一个类 myInt(带有 int myInteger),并且我想在 TreeSet 中使用它与自然排序不同的排序,我需要在我的类(myInt)中定义什么来做到这一点?

回答by NPE

How can I go over the objects in the Set

如何查看 Set 中的对象

The easiest way to iterate over the items of the set is like so:

迭代集合的项目的最简单方法是这样的:

SortedSet<T> set = new TreeSet<T>();
for (T elem : set) {
  // use elem
}

Can I keep the objects in the TreeSet sorted

我可以保持 TreeSet 中的对象排序吗

TreeSetis automatically sorted, so you don't need to do anything.

TreeSet会自动排序,因此您无需执行任何操作。

I have a class MyInt (with int myInteger) and I want to use it in TreeSet with a different ordering than the natural one

我有一个类 MyInt(带有 int myInteger),我想在 TreeSet 中使用它,它的顺序与自然顺序不同

You have two options:

您有两个选择:

Option 1: Make it implement Comparable<MyInt>:

选项 1:使其实施Comparable<MyInt>

public class MyInt implements Comparable<MyInt> {
   public int compareTo(MyInt o) {
     // return -1 if `this` is less than `o`
     //         0 if `this` is equal to `o`
     //         1 of `this` is greater than `o`
   }
}

Option 2: Supply a Comparator<MyInt>when constructing the TreeSet:

选项 2:Comparator<MyInt>在构建时提供一个TreeSet

public class MyIntCmp implements Comparator<MyInt> {
  // implement compare() and equals() as per Comparator javadoc
}
SortedSet<T> set = new TreeSet<T>(new MyIntCmp());

回答by MeBigFatGuy

for (MySetElementType element : mytreeset) {
}

TreeSet always keeps the objects sorted by how the objects compareTo (Comparable interface) is implemented. (Or you can pass a separate Comparator into the TreeSet constructor.)

TreeSet 始终按照对象 compareTo(Comparable 接口)的实现方式对对象进行排序。(或者您可以将单独的 Comparator 传递给 TreeSet 构造函数。)

回答by Android Killer

  1. The items inside TreeSet are automatically sorted according to their natural ordering if you are not giving your own comparator.
  2. Second thing you can define an Iterator to go through the TreeSet item directly without converting it to array.
  1. 如果您没有提供自己的比较器,则 TreeSet 中的项目会根据它们的自然顺序自动排序。
  2. 第二件事,您可以定义一个迭代器来直接遍历 TreeSet 项,而无需将其转换为数组。

回答by babsher

Since the SetTree implements iterable you can use a regular for each loop or use the iterator directly. In the javadocs it says that it will iterate in ascending order.

由于 SetTree 实现了 iterable,您可以为每个循环使用正则或直接使用迭代器。在 javadocs 中,它说它将按升序迭代。

http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html#iterator()

http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html#iterator()