ArrayList的subList方法带来的坑
最近在项目中遇到了一个问题,由一个对象序列化的结构,在反序列化时一直提示失败,真的百思不得其解啊。在对问题排查了好久之后,才发现是这个序列化的对象中的list调用了ArrayList的sublist方法存入导致的问题,真的是满满的坑,sublist还是要慎重使用的啊,下面详细介绍下sublist。
以下内容转自:ArrayList的subList方法
List接口中定义:
List<E> subList(int fromIndex, int toIndex);
英文注释:

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the
returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported by this list.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a
range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear(); Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via
the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in
progress may yield incorrect results.)

根据注释得知:
1,该方法返回的是父list的一个视图,从fromIndex(包含),到toIndex(不包含)。fromIndex=toIndex 表示子list为空
2,父子list做的非结构性修改(non-structural changes)都会影响到彼此:所谓的“非结构性修改”,是指不涉及到list的大小改变的修改。相反,结构性修改,指改变了list大小的修改。
3,对于结构性修改,子list的所有操作都会反映到父list上。但父list的修改将会导致返回的子list失效。
4,tips:如何删除list中的某段数据:
list.subList(from, to).clear();
示例代码:
来自【Java每日一题】20170105,就是看到这个题目才让我知道list的这个方法我没有接触过

package ques; import java.util.ArrayList;
import java.util.List; public class Ques0105 { public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a"); // 使用构造器创建一个包含list的列表list1
List<String> list1 = new ArrayList<String>(list);
// 使用subList生成与list相同的列表list2
List<String> list2 = list.subList(0, list.size());
list2.add("b"); System.out.println(list.equals(list1));
System.out.println(list.equals(list2));
}
}

返回结果如下:

可以发现,list2为list的子list,当list2发生结构性修改(list2.add("b"))后,list也发生相应改变,所以返回结果为false和true
ArrayList的subList方法带来的坑的更多相关文章
- ArrayList的subList方法
参考博文使用java.util.List.subList时最好小心点 List接口中定义: List<E> subList(int fromIndex, int toIndex); 英文注 ...
- 为什么要谨慎使用Arrays.asList、ArrayList的subList?
1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用: List<Integer> statusList = Arrays.asL ...
- 【Java 基础】Arrays.asList、ArrayList的subList注意事项
1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用: List<Integer> statusList = Arrays.asL ...
- 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...
- 谨慎使用ArrayList中的subList方法
转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wx ...
- ArrayList.subList方法使用总结
ArrayList.subList方法使用总结 示例 List<String> list=new ArrayList<>(); list.add("d"); ...
- List集合数据太多进行分批,List的subList方法应用
List<String> mStrings=new ArrayList<>(); //初始化 for (int i = 0; i < 1020; i++) { mStri ...
- iOS7之后经过滑动返回导航栏隐藏和显示带来的坑(转载)
iOS7之后经过滑动返回导航栏隐藏和显示带来的坑 Apple 自从iOS7之后增加了屏幕边缘右滑返回交互的支持,再配合UINavigationController的交换动画,pop上一级的操作变的非常 ...
- 需要注意的subList方法!和substring是不一样的!从源码解释他们的不同。
很多时候我们截取字符串用的是substring方法,很自然用着,但是对于列表的截取时很多时候就用得很少,但是其实他们是很不一样的,具体哪里不一样呢? package main; import java ...
随机推荐
- hadoop exit code 退出码含义
原文传送门:http://www.2cto.com/database/201308/236519.html "OS error code 1: Operation not permitted ...
- linux command lynx
[Purpose] Learning linux command lynx [Eevironment] Ubuntu 16.04 terminal apt-get ...
- 基于react开发package.json的配置
项目依赖 react网页开发的3件套: react, react-dom, react-router-dom, redux, react-reduxreact的UI组件库: antd(pc端), an ...
- UEditor在开发环境中正常运作,但是部署到Tomcat中却无法使用
背景 SpringBoot项目,在 JSP 中使用 UEditor 问题 UEditor 在开发环境中正常运作,但是导致部署到 Tomcat 中却无法使用 原因 在开发环境中,路径不够严谨,多 ...
- spark基础知识四
围绕spark的其他特性和应用.主要包括以下几个方面 spark自定义分区 spark中的共享变量 spark程序的序列化问题 spark中的application/job/stage/task之间的 ...
- java web开发入门二(struts)基于eclispe
JavaBean JavaBean, 咖啡豆. JavaBean是一种开发规范,可以说是一种技术. JavaBean就是一个普通的java类.只有符合以下规定才能称之为javabean: 1)必须提 ...
- 阿里云 azkaban 发邮件的坑
azkaban : 是一个 任务调度平台 安装文档: https://azkaban.readthedocs.io/en/latest/getStarted.html 因为阿里云是禁止掉是STMP 2 ...
- zookeeper shell
1.启动zk客户端 ./zkCli.sh -server 192.168.67.35:2182,192.168.67.36:2182,192.168.67.37:2182 2.创建zk节点 cre ...
- concurrent(七)ConcurrentHashMap源码分析
参考文档:https://www.cnblogs.com/xiaoxi/p/7474026.html https://www.ibm.com/developerworks/cn/java/java-l ...
- 【Gamma阶段】第八次Scrum Meeting
冰多多团队-Gamma阶段第八次Scrum会议 工作情况 团队成员 已完成任务 待完成任务 卓培锦 编辑器风格切换(添加夜间模式) UI界面手势切换 牛雅哲 添加pytorch训练dict和ssh工具 ...