简体   繁体   English

Java中的TreeSet

[英]TreeSet in Java

I understand Java best practice suggests that while declaring a variable the generic set interface is used to declare on the left and the specific implementation on the right. 我理解Java最佳实践表明,在声明变量时,通用集合接口用于在左侧声明,而特定实现在右侧。

Thus if I have to declare the Set interfaces, the right way is, 因此,如果我必须声明Set接口,正确的方法是,

Set<String> set = new TreeSet<>();

However with this declaration I'm not able to access the set.last() method. 但是有了这个声明,我无法访问set.last()方法。 However when I declare it this way, 但是,当我这样宣布时,

TreeSet<String> set = new TreeSet<>(); 

I can access, last() and first() . 我可以访问, last()first() Can someone help me understand why? 有人可以帮我理解为什么吗?

The last() and first() are specific methods belonging to TreeSet and not the generic interface Set . last()first()是属于TreeSet特定方法,而不是通用接口Set When you refer to the variable, it's looking at the source type and not the allocated type, therefore if you're storing a TreeSet as a Set , it may only be treated as a Set . 当您引用变量时,它会查看源类型而不是分配的类型,因此如果您将TreeSet存储为Set ,则只能将其视为Set This essentially hides the additional functionality. 这基本上隐藏了附加功能。

As an example, every class in Java extends Object . 例如,Java中的每个类都扩展了Object Therefore this is a totally valid allocation: 因此,这是一个完全有效的分配:

final Object mMyList = new ArrayList<String>();

However, we'll never be able to use ArrayList style functionality when referring directly to mMyList without applying type-casting , since all Java can tell is that it's dealing with a generic Object ; 但是,当直接引用mMyList而不应用类型转换时,我们将永远无法使用ArrayList样式功能,因为所有Java都可以告诉它它正在处理通用Object ; nothing more. 而已。

您可以使用SortedSet接口作为声明(具有first()last()方法)。

The interface of Set does not provide last() and first() because it does not always make sense for a Set . Set的接口不提供last()first()因为它对于Set并不总是有意义。

Java is a static typing language. Java是一种静态类型语言。 When compiler see you doing set.last() , as it is expecting set to be a Set . 当编译器看到你在做set.last() ,因为它期望setSet It will not know whether set is a TreeSet that provides last() , or it is a HashSet that does not. 它不知道set是否是提供last()TreeSet ,或者它是不是的HashSet That's why it is complaining. 这就是它抱怨的原因。

Hold on. 坚持,稍等。 You do not need to declare it using the concrete class TreeSet here. 您不需要在此处使用具体类TreeSet声明它。 TreeSet bears a SortedSet interface which provides such methods. TreeSet带有一个SortedSet接口,它提供了这样的方法。 In another word: because you need to work with a Set that is sorted (for which it make sense to provide first() and last() ), the interface that you should work against should be SortedSet instead of Set 换句话说:因为你需要使用一个已排序的Set (为了提供first()last()有意义的),你应该使用的接口应该是SortedSet而不是Set

Hence what you should be doing is 因此,你应该做的是

SortedSet<String> set = new TreeSet<>();

To add to the existing answers here, the reason is that TreeSet implements the interface NavigableSet which inherits from the interface java.util.SortedSet the methods comparator, first , last and spliterator. 要在此处添加现有答案,原因是TreeSet实现了NavigableSet接口,该接口继承自java.util.SortedSet接口的方法比较器, firstlast和spliterator。

Just the Set , on the other side, doesn't have SortedSet methods because it's not inherit from it. 另一方面, Set就没有SortedSet方法,因为它不是从它继承的。

在此输入图像描述

Check it out from more Java TreeSet examples . 从更多Java TreeSet示例中查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM