Java集合类框架源码分析 之 Queue接口源码解析 【10】

本文深入探讨Java Collection Framework中的Queue接口,分析其类简介与主要方法。强调了不要在Queue中插入Null,因为某些方法可能返回null造成混淆。另外,文章指出不能基于元素重写equals()和hashCode()方法,因为Queue包含了元素排序信息。接口定义的关键方法如add()和offer()的区别在于处理空间不足时的行为:add()会抛出异常,而offer()返回false。创建Queue对象时需指定容量,一旦确定则不再扩容,超过容量会抛出异常。
摘要由CSDN通过智能技术生成

在Java Collection Framework中,继承了Collection接口的主要有List,Queue,Set三大类接口。在上篇文章中,已经对List接口下的源码做了分析,本篇文章开始对Queue接口以及主要的实现类进行分析学习:

按照国际惯例,先看类简介,站得高才能看的远,居高临下,用一栏全局的视野来对Queue有一个大致的认识,对该类的学习会有很大的帮助。

来,翠花,上酸菜!

/*
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

package java.util;

/**
 * queue 涵盖了 Collection 的基本操作,并且额外提供了增加/获取/检查操作。这些方法都存在两种形式:当失败时抛出异常,或者返回特定的值null/false。
 * 后一种主要在容量受限时使用,在大多数实现中,插入操作不会出错。
 * A collection designed for holding elements prior to processing.
 * Besides basic {@link java.util.Collection Collection} operations,
 * queues provide additional insertion, extraction, and inspection
 * operations.  Each of these methods exists in two forms: one throws
 * an exception if the operation fails, the other returns a special
 * value (either {@code null} or {@code false}, depending on the
 * operation).  The latter form of the insert operation is designed
 * specifically for use with capacity-restricted {@code Queue}
 * implementations; in most implementations, insert operations cannot
 * fail.
 *
 * <table BORDER CELLPADDING=3 CELLSPACING=1>
 * <caption>Summary of Queue methods</caption>
 *  <tr>
 *    <td></td>
 *    <td ALIGN=CENTER><em>Throws exception</em></td>
 *    <td ALIGN=CENTER><em>Returns special value</em></td>
 *  </tr>
 *  <tr>
 *    <td><b>Insert</b></td>
 *    <td>{@link Queue#add add(e)}</td>
 *    <td>{@link Queue#offer offer(e)}</td>
 *  </tr>
 *  <tr>
 *    <td><b>Remove</b></td>
 *    <td>{@link Queue#remove remove()}</td>
 *    <td>{@link Queue#poll poll()}</td>
 *  </tr>
 *  <tr>
 *    <td><b>Examine</b></td>
 *    <td>{@link Queue#element element()}</td>
 *    <td>{@link Queue#peek peek()}</td>
 *  <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值