Java 集合框架的接口

原文:https://www.studytonight.com/java/collection-interfaces.php

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

收集接口

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

interface Collection <E>

收集接口方法

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

    | Method | Description | | Boolean Add (EOBJ) | is used to add objects to the collection. Returns true if obj is added to the collection. If obj is already a member of the collection, or the collection does not allow duplicates, false is returned. | | Boolean addition (set C) | Adds all elements of set C to the calling set. Returns true if an element is added. Otherwise, return false. | | Boolean removal (object object) | Removes an object from a set. Returns true if the element is removed. Otherwise, return false. | | Boolean remove all (set C) | Remove all elements of set C from the call set. Returns true if the elements of the collection are removed. Otherwise, return false. | | Boolean inclusion (object object) | Determines whether there is an object in the set. Returns true if obj is an element of the calling collection. Otherwise, return false. | | Boolean isempty () | Returns true if the set is empty, otherwise returns false. | | int size () | Returns the number of elements in a set. | | Empty () | Remove the total number of elements from the set. | | Object [] ToaArray () | Returns an array of elements of the call collection. | | Boolean reserved value (set c) | Delete all elements of the calling set except the specified set. | | Iterator iterator () | Returns the iterator of the calling set. | | Boolean equals (object object) | If the call set is equal to obj, it returns true. Otherwise, return false. | | Object [] to array (object array []) | Returns an array that contains only those set elements whose types match the specified array. |

列表界面

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

interface List <E>
  1. 根据位置允许随机访问和插入。
  2. 它允许复制元素。

列出接口方法

  1. Apart from methods of Collection Interface, Adds following methods of its own. [

    | method | describes | | object acquisition (integer index). | Returns the object | | object set stored at the specified index (integer index, Electronic object) | Store the object at the specified index in the call set | | int index of (objectobj) | Returns the index | | int lastIn the first object in the set. Dex (Objectobj) | Returns the index of the last occurrence of obj in the set | | List sub-list (int start, int end) | Returns the list containing elements between the start index and the end index in the set |

设置界面

该接口定义了一个集合。它扩展了集合接口,不允许插入重复元素。它的一般声明是,

interface Set <E>
  1. 它没有定义任何自己的方法。它有两个子界面,排序集导航集
  2. 排序设置界面扩展设置界面,按升序排列添加的元素。
  3. 导航设置界面扩展了排序设置界面,允许根据与给定值最接近的匹配检索元素。

Queue接口

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

interface Queue <E>

Queue接口方法

There are couple of new and interesting methods added by this interface. Some of them are mentioned in below table.

| 方法 | 描述 | | 对象轮询() | 删除队列头部的元素,如果队列为空,则返回空值 | | 对象移除() | 移除队列头部的元素,如果队列为空,则抛出nosucheelementexception | | 对象 peek() | 返回队列头部的元素,而不移除它。如果队列为空,返回空值 | | 对象元素() | 与 peek()相同,但如果队列为空,则抛出 NoSuchElementException | | 布尔报价 | 将对象添加到队列。 |

出列接口

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

interface Dequeue <E>
  1. 因为它实现了 Queue 接口,所以它具有与上面提到的相同的方法。
  2. 双端队列可以像标准栈一样作为简单队列运行。