【Java 集合】Collections 空列表细节处理

ops/2024/11/19 10:50:06/

问题

如下代码,虽然定义为非空 NonNull,但依然会返回空对象,导致调用侧被检测为空引用。

实际上不是Collections的问题是三目运算符返回了null对象。

import java.util.Collections;@NonNullprivate List<String> getInfo() {IccRecords iccRecords = mPhone.getIccRecords();if(iccRecords != null) {//省略逻辑String[] simSpdi = iccRecords.getServiceProviderDisplayInformation();return simSpdi != null ? Arrays.asList(simSpdi) : null; //根因是这里返回null}return Collections.EMPTY_LIST;}

源码案例

frameworks/opt/telephony/src/java/com/android/internal/telephony/cdnr/CarrierDisplayNameResolver.java

    @NonNullprivate List<String> getEfSpdi() {for (int i = 0; i < mEf.size(); i++) {if (mEf.valueAt(i).getServiceProviderDisplayInformation() != null) {return mEf.valueAt(i).getServiceProviderDisplayInformation();}}return Collections.EMPTY_LIST;}@NonNullprivate String getEfSpn() {for (int i = 0; i < mEf.size(); i++) {if (!TextUtils.isEmpty(mEf.valueAt(i).getServiceProviderName())) {return mEf.valueAt(i).getServiceProviderName();}}return "";}@NonNullprivate List<OperatorPlmnInfo> getEfOpl() {for (int i = 0; i < mEf.size(); i++) {if (mEf.valueAt(i).getOperatorPlmnList() != null) {return mEf.valueAt(i).getOperatorPlmnList();}}return Collections.EMPTY_LIST;}@NonNullprivate List<PlmnNetworkName> getEfPnn() {for (int i = 0; i < mEf.size(); i++) {if (mEf.valueAt(i).getPlmnNetworkNameList() != null) {return mEf.valueAt(i).getPlmnNetworkNameList();}}return Collections.EMPTY_LIST;}

代码解析

注意CollectionCollections是不同的。

  • libcore/ojluni/src/main/java/java/util/Collections.java 工具类
  • libcore/ojluni/src/main/java/java/util/Collection.java 接口

Collections中定义了内部类EmptyList,在静态List常量EMPTY_LIST(immutable不可变列表)初始化时会new一个没有指定类型的EmptyList。

public class Collections {/*** The empty list (immutable).  This list is serializable.** @see #emptyList()*/@SuppressWarnings("rawtypes")public static final List EMPTY_LIST = new EmptyList<>();/*** Returns an empty list (immutable).  This list is serializable.** <p>This example illustrates the type-safe way to obtain an empty list:* <pre>*     List&lt;String&gt; s = Collections.emptyList();* </pre>** @implNote* Implementations of this method need not create a separate {@code List}* object for each call.   Using this method is likely to have comparable* cost to using the like-named field.  (Unlike this method, the field does* not provide type safety.)** @param <T> type of elements, if there were any, in the list* @return an empty immutable list** @see #EMPTY_LIST* @since 1.5*/@SuppressWarnings("unchecked")public static final <T> List<T> emptyList() {return (List<T>) EMPTY_LIST;}/*** @serial include*/private static class EmptyList<E>extends AbstractList<E>implements RandomAccess, Serializable {@java.io.Serialprivate static final long serialVersionUID = 8842843931221139166L;public Iterator<E> iterator() {return emptyIterator();}public ListIterator<E> listIterator() {return emptyListIterator();}// Preserves singleton property@java.io.Serialprivate Object readResolve() {return EMPTY_LIST;}}}

解决方案

将 Collections.EMPTY_LIST 替换成 Collections.emptyList()。

虽然它们都可以用于表示一个空的不可变列表,但 Collections.emptyList() 是更优先的选择,因为它提供了类型安全性和更好的代码可读性。

附:Collections 源码

一些值得学习的语法,Android 扩展的排序跟Java 集合原生排序的实现差异

libcore/ojluni/src/main/java/java/util/Collections.java

import dalvik.system.VMRuntime;/*** This class consists exclusively of static methods that operate on or return* collections.  It contains polymorphic algorithms that operate on* collections, "wrappers", which return a new collection backed by a* specified collection, and a few other odds and ends.** <p>The methods of this class all throw a {@code NullPointerException}* if the collections or class objects provided to them are null.** <p>The documentation for the polymorphic algorithms contained in this class* generally includes a brief description of the <i>implementation</i>.  Such* descriptions should be regarded as <i>implementation notes</i>, rather than* parts of the <i>specification</i>.  Implementors should feel free to* substitute other algorithms, so long as the specification itself is adhered* to.  (For example, the algorithm used by {@code sort} does not have to be* a mergesort, but it does have to be <i>stable</i>.)** <p>The "destructive" algorithms contained in this class, that is, the* algorithms that modify the collection on which they operate, are specified* to throw {@code UnsupportedOperationException} if the collection does not* support the appropriate mutation primitive(s), such as the {@code set}* method.  These algorithms may, but are not required to, throw this* exception if an invocation would have no effect on the collection.  For* example, invoking the {@code sort} method on an unmodifiable list that is* already sorted may or may not throw {@code UnsupportedOperationException}.** <p>This class is a member of the* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">* Java Collections Framework</a>.** @author  Josh Bloch* @author  Neal Gafter* @see     Collection* @see     Set* @see     List* @see     Map* @since   1.2*/public class Collections {// Suppresses default constructor, ensuring non-instantiability.private Collections() {}// Algorithms// Android-added: List.sort() vs. Collections.sort() app compat.// Added a warning in the documentation.// Collections.sort() calls List.sort() for apps targeting API version >= 26// (Android Oreo) but the other way around for app targeting <= 25 (Nougat)./*** Sorts the specified list into ascending order, according to the* {@linkplain Comparable natural ordering} of its elements.* All elements in the list must implement the {@link Comparable}* interface.  Furthermore, all elements in the list must be* <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)}* must not throw a {@code ClassCastException} for any elements* {@code e1} and {@code e2} in the list).** <p>This sort is guaranteed to be <i>stable</i>:  equal elements will* not be reordered as a result of the sort.** <p>The specified list must be modifiable, but need not be resizable.** @implNote* This implementation defers to the {@link List#sort(Comparator)}* method using the specified list and a {@code null} comparator.* Do not call this method from {@code List.sort()} since that can lead* to infinite recursion. Apps targeting APIs {@code <= 25} observe* backwards compatibility behavior where this method was implemented* on top of {@link List#toArray()}, {@link ListIterator#next()} and* {@link ListIterator#set(Object)}.** @param  <T> the class of the objects in the list* @param  list the list to be sorted.* @throws ClassCastException if the list contains elements that are not*         <i>mutually comparable</i> (for example, strings and integers).* @throws UnsupportedOperationException if the specified list's*         list-iterator does not support the {@code set} operation.* @throws IllegalArgumentException (optional) if the implementation*         detects that the natural ordering of the list elements is*         found to violate the {@link Comparable} contract* @see List#sort(Comparator)*/public static <T extends Comparable<? super T>> void sort(List<T> list) {// Android-changed: List.sort() vs. Collections.sort() app compat.// Call sort(list, null) here to be consistent with that method's// (changed on Android) behavior.// list.sort(null);sort(list, null);}}

http://www.ppmy.cn/ops/134945.html

相关文章

ODC 如何精确呈现SQL耗时 | OceanBase 开发者工具解析

前言 在程序员或DBA的日常工作中&#xff0c;编写并执行SQL语句如同日常饮食中的一餐一饭&#xff0c;再寻常不过。然而&#xff0c;在使用命令行或黑屏客户端处理SQL时&#xff0c;常会遇到编写难、错误排查缓慢以及查询结果可读性不佳等难题&#xff0c;因此&#xff0c;图形…

redis和mongodb等对比分析

Redis 和 MongoDB 都是非常流行的 NoSQL 数据库,它们在数据存储模型、性能、扩展性等方面有很大的差异。下面是 Redis 和 MongoDB 的对比分析: 1. 数据模型 Redis: 键值存储:Redis 是一个内存数据结构存储,它支持多种数据类型,如字符串、哈希、列表、集合、有序集合等。…

前端项目接入单元测试手册

一、单元测试 Vue.js项目中的单元测试是一种软件测试方法&#xff0c;通过对最小的、可测试的代码单元进行检查和验证来保证代码质量。确保每个组件作为独立单元正确执行其预定功能。当代码库随着时间发展增长时&#xff0c;单元测试成为识别错误和避免潜在问题的关键手段。此…

打造专业问答社区:Windows部署Apache Answer结合cpolar实现公网访问

文章目录 前言1. 本地安装Docker2. 本地部署Apache Answer2.1 设置语言选择简体中文2.2 配置数据库2.3 创建配置文件2.4 填写基本信息 3. 如何使用Apache Answer3.1 后台管理3.2 提问与回答3.3 查看主页回答情况 4. 公网远程访问本地 Apache Answer4.1 内网穿透工具安装4.2 创建…

2022数学分析【南昌大学】

2022 数学分析 利用极限定义证明: lim ⁡ n → ∞ 4 n 3 + n − 2 2 n 3 − 10 = 2 \mathop {\lim }\limits_{n \to \infty } \frac{{4{n^3} + n - 2}}{{2{n^3} - 10}} = 2 n→∞lim​2n3−104n3+n−2​=2 ∀ ε > 0 \forall \varepsilon>0 ∀ε>0 要使不等式成立,…

Vue模块化开发的理解

Vue模块化是指在Vue.js开发中&#xff0c;将代码按功能拆分成多个独立的模块&#xff0c;以提高代码的可维护性、可读性和复用性。以下是对Vue模块化的详细理解&#xff1a; 一、Vue模块化的实现方式 组件化开发&#xff1a; Vue组件是模块化的基本单元&#xff0c;每个组件封…

江浙沪智慧城市行业盛会 | 2025杭州国际智慧城市展览会

智慧城市的概念逐渐深入人心&#xff0c;成为现代城市发展的重要方向。杭州&#xff0c;这座历史悠久而又充满活力的城市&#xff0c;正以其独特的魅力吸引着全球的目光。即将到来的2025年&#xff0c;杭州又将迎来一场盛大的智慧城市行业盛会——2025杭州国际智慧城市展览会。…

循环矩阵和BCCB矩阵与向量乘积的快速计算——矩阵向量乘积与频域乘积之间的转换

目录 循环矩阵循环矩阵的定义特征值与特征向量循环矩阵的对角化 循环矩阵与向量的乘积 BCCB矩阵BCCB矩阵的定义BCCB矩阵的对角化BCCB 矩阵与向量的乘积BCCB 矩阵与向量乘积的实现 总结 循环矩阵&#xff08;Circulant Matrix&#xff09;和块循环对称矩阵&#xff08;Block Cir…