网站设计到底做多宽,沧州网站建设专业定制,信息管理系统网站模板,江西网站制作的公司文章目录 1. Arrays.asList()获取到的ArrayList只能遍历#xff0c;不能增加或删除元素2. java.util.ArrayList.SubList有实现add()、remove()方法3. 遍历集合时对元素重新赋值、对元素中的属性赋值、删除元素、新增元素3.1 普通for循环3.2 增强for循环3.3 forEach循环3.4 str… 文章目录 1. Arrays.asList()获取到的ArrayList只能遍历不能增加或删除元素2. java.util.ArrayList.SubList有实现add()、remove()方法3. 遍历集合时对元素重新赋值、对元素中的属性赋值、删除元素、新增元素3.1 普通for循环3.2 增强for循环3.3 forEach循环3.4 stream forEach循环3.5 迭代器 4. 对大集合进行分组遍历分组后的大集合各个子集合使用完成后立即将元素删除 我们常说的ArrayList是指java.util.ArrayList 1. Arrays.asList()获取到的ArrayList只能遍历不能增加或删除元素 Arrays.asList()获取到的ArrayList不是java.util包下的而是java.util.Arrays.ArrayList 它是Arrays类自己定义的一个静态内部类这个内部类没有实现add()、remove()方法而是直接使用它的父类AbstractList的相应方法。而AbstractList中的add()和remove()是直接抛出java.lang.UnsupportedOperationException异常 public static void main(String[] args) {ListString list Arrays.asList(xin, liu, shijian);// 遍历 oklist.stream().forEach(System.out::println);for (int i 0; i list.size(); i) {// 报异常UnsupportedOperationExceptionlist.add(haha);// 报异常UnsupportedOperationExceptionlist.remove(i);}
}2. java.util.ArrayList.SubList有实现add()、remove()方法 java.util.ArrayList.SubList 是ArrayList的内部类可以add 和 remove 不建议对得到的子集合进行增、删操作 public static void main(String[] args) {ListPersonDTO list getDataList();ListPersonDTO subList list.subList(0, 2);int size subList.size();for (int i 0; i size; i) {PersonDTO personDTO subList.get(i);if (i 0) {
// subList.remove(personDTO);subList.add(personDTO);break;}}System.out.println(list);}3. 遍历集合时对元素重新赋值、对元素中的属性赋值、删除元素、新增元素 遍历方式普通for循环、增强for循环、forEach、stream forEach、迭代器 对元素重新赋值各种遍历方式都做不到对元素中的属性赋值各种遍历方式都能做到对集合新增元素普通for循环可以做到其他遍历方式都做不到对集合删除元素普通for循环和迭代器可以做到其他方式都做不到 建议遍历集合时删除元素用迭代器、新增元素可以新建一个集合 准备实验数据 private static ListPersonDTO getDataList() {ListPersonDTO list new ArrayList();PersonDTO p1 new PersonDTO();p1.setPersonName(xiaohua1);PersonDTO p2 new PersonDTO();p2.setPersonName(xiaohua2);PersonDTO p3 new PersonDTO();p3.setPersonName(xiaohua3);PersonDTO p4 new PersonDTO();p4.setPersonName(xiaohua4);PersonDTO p5 new PersonDTO();p5.setPersonName(xiaohua5);PersonDTO p6 new PersonDTO();p6.setPersonName(xiaohua6);list.add(p1);list.add(p2);list.add(p3);list.add(p4);list.add(p5);list.add(p6);return list;}3.1 普通for循环 普通for循环遍历元素时重新赋值失败 public static void main(String[] args) {ListPersonDTO list getDataList();for (int i 0; i list.size(); i) {// 原因是这里personDTO是另一个栈变量并不会对集合中的栈内容对象在内存中的地址进行改变PersonDTO personDTO list.get(i);personDTO null;}System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, agenull), PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua3, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua5, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull)] 普通for循环遍历元素时对元素中的属性成功赋值 public static void main(String[] args) {ListPersonDTO list getDataList();for (int i 0; i list.size(); i) {// 成功是因为它们虽然是不同的变量但栈内容相同都是同一个对象的内存地址这里会更改到堆中对象的内容PersonDTO personDTO list.get(i);personDTO.setAge(5);}System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, age5), PersonDTO(bookEntityListnull, personNamexiaohua2, age5), PersonDTO(bookEntityListnull, personNamexiaohua3, age5), PersonDTO(bookEntityListnull, personNamexiaohua4, age5), PersonDTO(bookEntityListnull, personNamexiaohua5, age5), PersonDTO(bookEntityListnull, personNamexiaohua6, age5)] 普通for循环遍历时删除元素ok public static void main(String[] args) {ListPersonDTO list getDataList();for (int i 0; i list.size(); i) {PersonDTO personDTO list.get(i);list.remove(personDTO);}System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull)] 普通for循环遍历时新增元素size若不固定报异常OutOfMemoryError public static void main(String[] args) {ListPersonDTO list getDataList();for (int i 0; i list.size(); i) {PersonDTO personDTO list.get(i);list.add(personDTO);System.out.println(list.size(): list.size());}System.out.println(list);}打印结果 Exception in thread “main” java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3210) at java.util.Arrays.copyOf(Arrays.java:3181) at java.util.ArrayList.grow(ArrayList.java:267) at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:241) at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:233) at java.util.ArrayList.add(ArrayList.java:464) 后来又执行一次到这个数据量还未结束 list.size(): 43014827 list.size(): 43014828 list.size(): 43014829 Process finished with exit code 130 Java VisualVM监视图如下 换种写法固定size值就运行ok了普通for循环遍历时就可以新增元素了 public static void main(String[] args) {ListPersonDTO list getDataList();int size list.size();for (int i 0; i size; i) {PersonDTO personDTO list.get(i);list.add(personDTO);System.out.println(list.size(): list.size());}System.out.println(list);}打印结果 list.size(): 7 list.size(): 8 list.size(): 9 list.size(): 10 list.size(): 11 list.size(): 12 [PersonDTO(bookEntityListnull, personNamexiaohua1, agenull), PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua3, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua5, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull), PersonDTO(bookEntityListnull, personNamexiaohua1, agenull), PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua3, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua5, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull)] 3.2 增强for循环 增强for循环遍历元素时重新赋值失败 public static void main(String[] args) {ListPersonDTO list getDataList();for (PersonDTO dto : list) {// 增强for循环内部实现是迭代器我认为是调用了新方法进行了值传递dto是另一个变量了dto null;}System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, agenull), PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua3, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua5, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull)] 增强for循环遍历元素时对元素中的属性成功赋值 public static void main(String[] args) {ListPersonDTO list getDataList();for (PersonDTO dto : list) {dto.setAge(7);}System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, age7), PersonDTO(bookEntityListnull, personNamexiaohua2, age7), PersonDTO(bookEntityListnull, personNamexiaohua3, age7), PersonDTO(bookEntityListnull, personNamexiaohua4, age7), PersonDTO(bookEntityListnull, personNamexiaohua5, age7), PersonDTO(bookEntityListnull, personNamexiaohua6, age7)] 增强for循环遍历时删除元素报异常ConcurrentModificationException public static void main(String[] args) {ListPersonDTO list getDataList();for (PersonDTO dto : list) {list.remove(dto);}System.out.println(list);}打印结果 Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList I t r . c h e c k F o r C o m o d i f i c a t i o n ( A r r a y L i s t . j a v a : 911 ) a t j a v a . u t i l . A r r a y L i s t Itr.checkForComodification(ArrayList.java:911) at java.util.ArrayList Itr.checkForComodification(ArrayList.java:911)atjava.util.ArrayListItr.next(ArrayList.java:861) 增强for循环遍历时新增元素报异常ConcurrentModificationException public static void main(String[] args) {ListPersonDTO list getDataList();for (PersonDTO dto : list) {list.add(dto);}System.out.println(list);}打印结果 Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList I t r . c h e c k F o r C o m o d i f i c a t i o n ( A r r a y L i s t . j a v a : 911 ) a t j a v a . u t i l . A r r a y L i s t Itr.checkForComodification(ArrayList.java:911) at java.util.ArrayList Itr.checkForComodification(ArrayList.java:911)atjava.util.ArrayListItr.next(ArrayList.java:861) 3.3 forEach循环 forEach循环遍历元素时重新赋值失败 public static void main(String[] args) {ListPersonDTO list getDataList();// forEach循环内部实现是匿名内部类调用了函数式接口的新方法进行了值传递dto是另一个变量了list.forEach(dto - dto null);System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, agenull), PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua3, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua5, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull)] forEach循环遍历元素时对元素中的属性成功赋值 public static void main(String[] args) {ListPersonDTO list getDataList();list.forEach(dto - dto.setAge(6));System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, age6), PersonDTO(bookEntityListnull, personNamexiaohua2, age6), PersonDTO(bookEntityListnull, personNamexiaohua3, age6), PersonDTO(bookEntityListnull, personNamexiaohua4, age6), PersonDTO(bookEntityListnull, personNamexiaohua5, age6), PersonDTO(bookEntityListnull, personNamexiaohua6, age6)] forEach循环遍历时删除元素报异常ConcurrentModificationException public static void main(String[] args) {ListPersonDTO list getDataList();list.forEach(dto - list.remove(dto));System.out.println(list);}打印结果 Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList.forEach(ArrayList.java:1262) forEach循环遍历时新增元素报异常ConcurrentModificationException public static void main(String[] args) {ListPersonDTO list getDataList();list.forEach(dto - list.add(new PersonDTO()));System.out.println(list);}打印结果 Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList.forEach(ArrayList.java:1262) 3.4 stream forEach循环 stream forEach循环遍历元素时重新赋值失败 public static void main(String[] args) {ListPersonDTO list getDataList();// stream forEach循环内部实现是匿名内部类调用了函数式接口的新方法进行了值传递dto是另一个变量了list.stream().forEach(dto - dto null);System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, agenull), PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua3, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua5, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull)] stream forEach循环遍历元素时对元素中的属性成功赋值 public static void main(String[] args) {ListPersonDTO list getDataList();list.stream().forEach(dto - dto.setAge(8));System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, age8), PersonDTO(bookEntityListnull, personNamexiaohua2, age8), PersonDTO(bookEntityListnull, personNamexiaohua3, age8), PersonDTO(bookEntityListnull, personNamexiaohua4, age8), PersonDTO(bookEntityListnull, personNamexiaohua5, age8), PersonDTO(bookEntityListnull, personNamexiaohua6, age8)] stream forEach循环遍历时删除元素报异常ConcurrentModificationException public static void main(String[] args) {ListPersonDTO list getDataList();list.stream().forEach(dto - list.remove(dto));System.out.println(list);}打印结果 Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList.forEach(ArrayList.java:1262) stream forEach循环遍历时新增元素报异常ConcurrentModificationException public static void main(String[] args) {ListPersonDTO list getDataList();list.stream().forEach(dto - list.add(new PersonDTO()));System.out.println(list);}打印结果 Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList A r r a y L i s t S p l i t e r a t o r . f o r E a c h R e m a i n i n g ( A r r a y L i s t . j a v a : 1390 ) a t j a v a . u t i l . s t r e a m . R e f e r e n c e P i p e l i n e ArrayListSpliterator.forEachRemaining(ArrayList.java:1390) at java.util.stream.ReferencePipeline ArrayListSpliterator.forEachRemaining(ArrayList.java:1390)atjava.util.stream.ReferencePipelineHead.forEach(ReferencePipeline.java:580) 3.5 迭代器 迭代器循环遍历元素时重新赋值失败 public static void main(String[] args) {ListPersonDTO list getDataList();IteratorPersonDTO iterator list.iterator();while (iterator.hasNext()) {// 原因是这里dto是另一个栈变量并不会对集合中的栈内容对象在内存中的地址进行改变PersonDTO dto iterator.next();dto null;}System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, agenull), PersonDTO(bookEntityListnull, personNamexiaohua2, agenull), PersonDTO(bookEntityListnull, personNamexiaohua3, agenull), PersonDTO(bookEntityListnull, personNamexiaohua4, agenull), PersonDTO(bookEntityListnull, personNamexiaohua5, agenull), PersonDTO(bookEntityListnull, personNamexiaohua6, agenull)] 迭代器循环遍历元素时对元素中的属性成功赋值 public static void main(String[] args) {ListPersonDTO list getDataList();IteratorPersonDTO iterator list.iterator();while (iterator.hasNext()) {PersonDTO dto iterator.next();dto.setAge(9);}System.out.println(list);}打印结果 [PersonDTO(bookEntityListnull, personNamexiaohua1, age9), PersonDTO(bookEntityListnull, personNamexiaohua2, age9), PersonDTO(bookEntityListnull, personNamexiaohua3, age9), PersonDTO(bookEntityListnull, personNamexiaohua4, age9), PersonDTO(bookEntityListnull, personNamexiaohua5, age9), PersonDTO(bookEntityListnull, personNamexiaohua6, age9)] 迭代器遍历时删除元素可以成功 public static void main(String[] args) {ListPersonDTO list getDataList();IteratorPersonDTO iterator list.iterator();while (iterator.hasNext()) {PersonDTO dto iterator.next();iterator.remove();}System.out.println(list);}打印结果 [] 迭代器遍历时新增元素报异常ConcurrentModificationException public static void main(String[] args) {ListPersonDTO list getDataList();IteratorPersonDTO iterator list.iterator();while (iterator.hasNext()) {PersonDTO dto iterator.next();list.add(new PersonDTO());}System.out.println(list);}打印结果 Exception in thread “main” java.util.ConcurrentModificationException at java.util.ArrayList I t r . c h e c k F o r C o m o d i f i c a t i o n ( A r r a y L i s t . j a v a : 911 ) a t j a v a . u t i l . A r r a y L i s t Itr.checkForComodification(ArrayList.java:911) at java.util.ArrayList Itr.checkForComodification(ArrayList.java:911)atjava.util.ArrayListItr.next(ArrayList.java:861) 4. 对大集合进行分组遍历分组后的大集合各个子集合使用完成后立即将元素删除 场景集合中对象比较多可能造成OOM集合中的一部分元素使用完成后立即删除 import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;public class ArrayListDemo {private static final int SUBLIST_SIZE 2;public static void main(String[] args) {// 不适合list中有元素减少的场景// 1.得到总list这个时候的list接下来还可能会继续扩展ListPersonDTO list getDataList();System.out.println(刚开始, list.size list.size());// 2.业务逻辑代码list还可能会扩展// 3.处理完子集合就删除它的元素deleteSubList(list);System.out.println(处理一波后, list.size list.size());// 4.list不再扩展删除剩下元素也是一个一个子集合的删除元素deleteSubList(list, false);System.out.println(最后处理后, list.size list.size());}private static void deleteSubList(ListPersonDTO list) {// isNotEnd 代表list集合可能还会增加元素deleteSubList(list, true);}private static void deleteSubList(ListPersonDTO list, boolean isNotEnd) {if (Objects.isNull(isNotEnd)) {isNotEnd false;}if (CollectionUtils.isNotEmpty(list) ((list.size() SUBLIST_SIZE) || !isNotEnd)) {int size list.size() / SUBLIST_SIZE;if ((list.size() % SUBLIST_SIZE) 0) {size;}for (int i 0; i size; i) {if (CollectionUtils.isNotEmpty(list)) {ListPersonDTO subList Lists.partition(list, SUBLIST_SIZE).get(0);// 不再继续处理业务逻辑: list中的数据量小于SUBLIST_SIZE list中还可能增加元素if ((list.size() SUBLIST_SIZE) isNotEnd) {break;}// 使用subList处理业务逻辑// 删出list中的subListIteratorPersonDTO iterator list.iterator();int j 0;while (iterator.hasNext()) {iterator.next();j;if (j SUBLIST_SIZE) {iterator.remove();}if (j SUBLIST_SIZE) {break;}}System.out.println(删除subList后, list.size list.size());}}}}private static ListPersonDTO getDataList() {ListPersonDTO list new ArrayList();PersonDTO p1 new PersonDTO();p1.setPersonName(xiaohua1);PersonDTO p2 new PersonDTO();p2.setPersonName(xiaohua2);PersonDTO p3 new PersonDTO();p3.setPersonName(xiaohua3);PersonDTO p4 new PersonDTO();p4.setPersonName(xiaohua4);PersonDTO p5 new PersonDTO();p5.setPersonName(xiaohua5);PersonDTO p6 new PersonDTO();p6.setPersonName(xiaohua6);PersonDTO p7 new PersonDTO();p7.setPersonName(xiaohua7);list.add(p1);list.add(p2);list.add(p3);list.add(p4);list.add(p5);list.add(p6);list.add(p7);return list;}
} 打印结果 刚开始, list.size 7 删除subList后, list.size 5 删除subList后, list.size 3 删除subList后, list.size 1 处理一波后, list.size 1 删除subList后, list.size 0 最后处理后, list.size 0