Java Collection Framework的接口

The Collections framework has a lot of Interfaces, setting the fundamental nature of various collection classes. Lets study the most important Interfaces in the Collections framework.

Collections框架具有很多接口,设置了各种集合类的基本性质。 让我们研究集合框架中最重要的接口。

收集界面 (The Collection Interface)

It is at the top of collection heirarchy and must be implemented by any class that defines a collection. Its general declaration is,

它在集合层次结构的顶部,并且必须由定义集合的任何类来实现。 它的一般声明是

interface Collection <E>

采集接口方法 (Collection Interface Methods)

  1. Following are some of the commonly used methods in this interface.

    以下是此界面中的一些常用方法。

    MethodsDescription
    boolean add( E obj )Used to add objects to a collection. Returns true if obj was added to the collection. Returns false if obj is already a member of the collection, or if the collection does not allow duplicates.
    boolean addAll( Collection C )Add all elements of collection C to the invoking collection. Returns true if the element were added. Otherwise, returns false.
    boolean remove( Object obj )To remove an object from collection. Returns true if the element was removed. Otherwise, returns false.
    boolean removeAll( Collection C )Removes all element of collection C from the invoking collection. Returns true if the collection's elements were removed. Otherwise, returns false.
    boolean contains( Object obj )To determine whether an object is present in collection or not. Returns true if obj is an element of the invoking collection. Otherwise, returns false.
    boolean isEmpty()Returns true if collection is empty, else returns false.
    int size()Returns number of elements present in collection.
    void clear()Removes total number of elements from the collection.
    Object[] toArray()Returns an array which consists of the invoking collection elements.
    boolean retainAll(Collection c)Deletes all the elements of invoking collection except the specified collection.
    Iterator iterator( )Returns an iterator for the invoking collection.
    boolean equals(Object obj)Returns true if the invoking collection and obj are equal. Otherwise, returns false.
    Object[] toArray(Object array[])Returns an array containing only those collection elements whose type matches of the specified array.
    方法 描述
    布尔值add(E obj) 用于将对象添加到集合中。 如果将obj添加到集合中,则返回true。 如果obj已经是集合的成员,或者该集合不允许重复,则返回false。
    布尔addAll(集合C) 将集合C的所有元素添加到调用集合。 如果添加了元素,则返回true。 否则,返回false。
    布尔值remove(Object obj) 从集合中删除对象。 如果该元素已删除,则返回true。 否则,返回false。
    布尔值removeAll(集合C) 从调用集合中删除集合C的所有元素。 如果删除了集合的元素,则返回true。 否则,返回false。
    布尔contains(Object obj) 确定对象是否存在于集合中。 如果obj是调用集合的元素,则返回true。 否则,返回false。
    布尔isEmpty() 如果collection为空,则返回true,否则返回false。
    int size() 返回集合中存在的元素数。
    无效clear() 从集合中删除元素总数。
    Object [] toArray() 返回一个由调用集合元素组成的数组。
    boolean keepAll(Collection c) 删除调用集合中除指定集合之外的所有元素。
    迭代器iterator() 返回调用集合的迭代器。
    布尔值equals(Object obj) 如果调用集合和obj相等,则返回true。 否则,返回false。
    Object [] toArray(Object array []) 返回一个仅包含类型与指定数组匹配的集合元素的数组。

列表界面 (The List Interface)

It extends the Collection Interface, and defines storage as sequence of elements. Following is its general declaration,

它扩展了Collection接口,并将存储定义为元素序列。 以下是其一般声明,

interface List <E>
  1. Allows random access and insertion, based on position.

    允许根据位置随机访问和插入。

  2. It allows Duplicate elements.

    它允许重复元素。

  3. 列表界面方法 (List Interface Methods)

  4. Apart from methods of Collection Interface, it adds following methods of its own.

    除了Collection接口的方法外,它还添加了以下自己的方法。

    MethodsDescription
    Object get( int index )Returns object stored at the specified index
    Object set( int index, E obj)Stores object at the specified index in the calling collection
    int indexOf( Object obj )Returns index of first occurrence of obj in the collection
    int lastIndexOf( Object obj )Returns index of last occurrence of obj in the collection
    List subList( int start, int end )Returns a list containing elements between start and end index in the collection
    方法 描述
    对象get(int index) 返回存储在指定索引处的对象
    对象集(int index,E obj) 将对象存储在调用集合中的指定索引处
    int indexOf(Object obj) 返回集合中obj首次出现的索引
    int lastIndexOf(Object obj) 返回集合中最后一次出现的obj的索引
    列表subList(int start,int end) 返回一个列表,其中包含集合中开始索引和结束索引之间的元素

设置界面 (The Set Interface)

This interface defines a Set. It extends Collection interface and doesn't allow insertion of duplicate elements. It's general declaration is,

此接口定义一个Set。 它扩展了Collection接口,并且不允许插入重复的元素。 它的一般声明是,

interface Set <E>
  1. It doesn't define any method of its own. It has two sub interfaces, SortedSet and NavigableSet.

    它没有定义自己的任何方法。 它具有两个子接口, SortedSetNavigableSet

  2. SortedSet interface extends Set interface and arranges added elements in an ascending order.

    SortedSet接口扩展了Set接口,并按升序排列添加的元素。

  3. NavigabeSet interface extends SortedSet interface, and allows retrieval of elements based on the closest match to a given value or values.

    NavigabeSet接口扩展了SortedSet接口,并允许根据与给定值或最接近值的最接近匹配来检索元素。

队列接口 (The Queue Interface)

It extends collection interface and defines behaviour of queue, that is first-in, first-out. It's general declaration is,

它扩展了收集接口并定义了队列的行为,即先进先出。 它的一般声明是,

interface Queue <E>

队列接口方法 (Queue Interface Methods)

此接口添加了两个有趣的新方法。 下表中提到了其中一些。
MethodsDescription
Object poll()removes element at the head of the queue and returns null if queue is empty
Object remove()removes element at the head of the queue and throws NoSuchElementException if queue is empty
Object peek()returns the element at the head of the queue without removing it. Returns null if queue is empty
Object element()same as peek(), but throws NoSuchElementException if queue is empty
boolean offer( E obj )Adds object to queue.
方法 描述
对象poll() 删除队列开头的元素,如果队列为空,则返回null
对象remove() 如果队列为空,则删除队列开头的元素并引发NoSuchElementException
对象偷看() 在不删除元素的情况下返回队列开头的元素。 如果队列为空,则返回null
对象element() 与peek()相同,但是如果队列为空,则抛出NoSuchElementException
boolean offer(E obj) 将对象添加到队列。

出队接口 (The Dequeue Interface)

It extends Queue interface and implements behaviour of a double-ended queue. Its general declaration is,

它扩展了Queue接口并实现了双端队列的行为。 它的一般声明是

interface Dequeue <E>
  1. Since it implements Queue interface, it has the same methods as mentioned there.

    由于它实现了Queue接口,因此具有与此处提到的方法相同的方法。

  2. Double ended queues can function as simple queues as well as like standard Stacks.

    双端队列既可以充当简单队列,也可以充当标准堆栈。

翻译自: https://www.studytonight.com/java/collection-interfaces.php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值