Java 集合框架(Queue 接口)

2018-11-22  本文已影响0人  6ea566508d0d

Queue 接口简介

对于 Queue 接口,Java的官方文只有这样一句话:

A Queue is a collection for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, removal, and inspection operations.

大概讲了两点:

接着讲述了 Queue 接口的一些规则:(这里和原文档的顺序稍有不同)

Queues typically, but not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to their values. Whatever ordering is used, the head of the queue is the element that would be removed by a call to remove or poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.
It is possible for a Queue implementation to restrict the number of elements that it holds; such queues are known as bounded. Some Queue implementations in java.util.concurrent are bounded, but the implementations in java.util are not.

这里简单理解一下就好了:

Queue 接口相关操作

Queue 接口相关操作,文档中有这样一段描述:

Each Queue method exists in two forms: (1) one throws an exception if the operation fails, and (2) the other returns a special value if the operation fails (either null or false, depending on the operation).

简单概述下:
Queue 接口的每个方法有两种形式:

Type of Operation Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

下面是对每个方法的简单说明:


更多参考:The Queue Interface (The Java™ Tutorials > Collections > Interfaces)

上一篇 下一篇

猜你喜欢

热点阅读