简体   繁体   English

为什么List接口扩展Collection接口?

[英]Why does List interface extend Collection interface?

The Collection interface has multiple methods. Collection接口有多种方法。 The List interface extends the Collection interface. List接口扩展了Collection接口。 It declares the same methods as the Collection interface? 它声明与Collection接口相同的方法? Why is this so? 为什么会这样?

For example 例如

interface Collection extends Iterable
{
     public abstract int size();
 public abstract boolean isEmpty();
 public abstract boolean contains(java.lang.Object);
 public abstract java.util.Iterator<E> iterator();
 public abstract java.lang.Object[] toArray();
 public abstract <T extends java/lang/Object> T[] toArray(T[]);
 public abstract boolean add(E);
 public abstract boolean remove(java.lang.Object);
 public abstract boolean containsAll(java.util.Collection<?>);
 public abstract boolean addAll(java.util.Collection<? extends E>);
 public abstract boolean removeAll(java.util.Collection<?>);
 public abstract boolean retainAll(java.util.Collection<?>);
 public abstract void clear();
 public abstract boolean equals(java.lang.Object);
 public abstract int hashCode();
}

and same methods are also present in List interface: List接口中也存在相同的方法:

public interface List extends Collection
{
 public abstract int size();
 public abstract boolean isEmpty();
 public abstract boolean contains(java.lang.Object);
 public abstract java.util.Iterator<E> iterator();
 public abstract java.lang.Object[] toArray();
 public abstract <T extends java/lang/Object> T[] toArray(T[]);
 public abstract boolean add(E);
 public abstract boolean remove(java.lang.Object);
 public abstract boolean containsAll(java.util.Collection<?>);
 public abstract boolean addAll(java.util.Collection<? extends E>);
 public abstract boolean removeAll(java.util.Collection<?>);
 public abstract boolean retainAll(java.util.Collection<?>);
 public abstract void clear();
 public abstract boolean equals(java.lang.Object);
 public abstract int hashCode();
}

Is it a requirement to write these methods again in List if it is already extending the Collection interface? 如果已经扩展Collection接口,是否需要在List中再次编写这些方法?

They're re-written so that they can be documented, in order to specify how the List refines the contract of these methods compared to the contract specified in the Collection interface. 它们被重写以便可以对它们进行记录,以便指定List如何与Collection接口中指定的契约相比改进这些方法的契约。

For example, the add() method in List is documented to specify that the element is added to the end of the list. 例如,记录Listadd()方法以指定将元素添加到列表的末尾。 This can't be specified in Collection, since a Collection doesn't have a beginning and an end. 这不能在Collection中指定,因为Collection没有开头和结尾。

JavaDoc and API contracts change somewhat/ or become more specific, as you move down the inheritance heirarchy. 随着你继承heirarchy,JavaDoc和API契约会有所改变/或变得更具体。

List re-declares these methods & gives them more specific JavaDoc. List重新声明这些方法并为它们提供更具体的JavaDoc。

Just for convenience. 只是为了方便。

Same mentioned in Docs 在Docs中也提到过

The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. List接口在迭代器,add,remove,equals和hashCode方法的契约上放置了除Collection接口中指定的规则之外的其他规定。 Declarations for other inherited methods are also included here for convenience 为方便起见,此处还包含其他继承方法的声明

Collection<T> is just a group of item. Collection<T>只是一组项目。 In itself it doesn't have any more requirements than holding references to the many items that are its members. 就其本身而言,除了保留对其成员的许多项目的引用之外,它没有任何其他要求。

In the basic java api there is two main types of collections: List<T> and Set<T> . 在基本的java api中,有两种主要类型的集合: List<T>Set<T>

List<T> have the extra requirement to maintain a certain order (insertion order, sorted order, ...) for all its items. List<T>有额外的要求来维护其所有项目的特定顺序(插入顺序,排序顺序......)。 So that if you request item N the list will always return the same item for N. 因此,如果您请求项目N,列表将始终返回N的相同项目。

Set<T> does not offer any guarantee on order but offers guarantee on uniqueness of the items. Set<T>不对订单提供任何保证,但保证物品的唯一性。 An item A cannot be added twice to a Set, or will appear only once in a set. 项目A不能两次添加到一个集合中,或者只能在一个集合中出现一次。

You should acquaint yourself with the practice of "marker" interface. 你应该熟悉“标记”界面的做法。 Serializable is one of those, and generally the basic example when talking about this. Serializable是其中之一,通常是谈论这个时的基本例子。 And List<T> and Set<T> are declared as such, they mark a collection as one or the other in order to inform the programmer of the behaviour they can expect from the collection they receive. 并且List<T>Set<T>被声明为这样,它们将集合标记为一个或另一个,以便通知程序员他们可以从他们收到的集合中获得的行为。

Please refer to Item 37 (Chapter 6) of "Effective Java" for a very good explanation on how this is better than using annotations. 有关如何比使用注释更好的解释,请参阅“有效Java”的第37项(第6章)。

And there is also the fact that myCollection instanceof MyInterface is faster than myCollection.getClass().isAnnotationPresent(MyAnnotation.class) or myCollection.getClass().getAnnotation(MyAnnotation.class) != null . 还有一个事实是myCollection instanceof MyInterfacemyCollection.getClass().isAnnotationPresent(MyAnnotation.class)myCollection.getClass().isAnnotationPresent(MyAnnotation.class)myCollection.getClass().getAnnotation(MyAnnotation.class) != null

The signatures on the toArray methods suggest that you extracted this from the compiled .class files. toArray方法上的签名表明您从已编译的.class文件中提取了该签名。 The class file format specifies that the .class file does not repeat methods inherited from superinterfaces, so I suspect that whatever tool you used to obtain these was showing you a composite view; 类文件格式指定.class文件不重复从超接口继承的方法,因此我怀疑您用于获取这些的任何工具都向您显示复合视图; the methods aren't actually physically present on List . 这些方法实际上并不存在于List

A collection is just a collection of items. 集合只是项目的集合

A list, apart from holding list of items, adds information regarding sequence of stuff to it. 除了保存项目列表之外,列表还向其添加有关项目顺序的信息。

When you add an item to collection, you are just adding it. 将项目添加到集合时,只需添加它即可。 When you add an item to List, you can add at position n 向List添加项目时,可以在位置n处添加

When you remove an item from collection, you are just removing it. 从集合中删除项目时,您只是将其删除。 When you remove an item from List, you can remove at position n 从列表中删除项目时,可以从位置n删除

When you want to get an item from collection, you have to iterate. 如果要从集合中获取项目,则必须进行迭代。 When you want to get an item from List, you can get at position n 当你想从List获得一个项目时,你可以到达位置n

首先接口List继承所有Collection方法,因此Collection接口中存在的所有方法也将存在于List接口中,但List接口有额外的方法,(由ur self检查)描述列表的行为

It is mainly because of documentation purpose they have used like that. 这主要是因为他们这样使用的文档目的。

For example 例如

Collection#retainAll 

Retains only the elements in this collection that are contained in the specified collection (optional operation). 仅保留此集合中包含在指定集合中的元素(可选操作)。

List#retainAll

Retains only the elements in this list that are contained in the specified collection (optional operation). 仅保留此列表中包含在指定集合中的元素(可选操作)。

Only for java doc purpose they have used like that. 只为了java doc目的,他们就是这样使用的。 But some of the methods behaviour itself changed. 但是一些方法行为本身也发生了变化。

For ex.add,remove

Method remove 方法删除

In List, Removes the first occurrence of the specified element from this list, if it is present (optional operation).

In Collection , Removes a single instance of the specified element from this collection, if it is present (optional operation).

Through Java doc they have clearly indicate List implementations are ordered. 通过Java doc,他们清楚地表明List实现是有序的。

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

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