Java ArrayList在遍历时删除元素

2024-02-07 20:50

本文主要是介绍Java ArrayList在遍历时删除元素,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 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) {List<String> 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) {List<PersonDTO> list = getDataList();List<PersonDTO> 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 List<PersonDTO> getDataList() {List<PersonDTO> 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) {List<PersonDTO> list = getDataList();for (int i = 0; i < list.size(); i++) {// 原因是这里personDTO是另一个栈变量,并不会对集合中的栈内容(对象在内存中的地址)进行改变PersonDTO personDTO = list.get(i);personDTO = null;}System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=null), PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua3, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua5, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null)]


普通for循环,遍历元素时,对元素中的属性成功赋值

    public static void main(String[] args) {List<PersonDTO> list = getDataList();for (int i = 0; i < list.size(); i++) {// 成功是因为它们虽然是不同的变量,但栈内容相同,都是同一个对象的内存地址,这里会更改到堆中对象的内容PersonDTO personDTO = list.get(i);personDTO.setAge(5);}System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=5), PersonDTO(bookEntityList=null, personName=xiaohua2, age=5), PersonDTO(bookEntityList=null, personName=xiaohua3, age=5), PersonDTO(bookEntityList=null, personName=xiaohua4, age=5), PersonDTO(bookEntityList=null, personName=xiaohua5, age=5), PersonDTO(bookEntityList=null, personName=xiaohua6, age=5)]


普通for循环遍历时删除元素,ok

    public static void main(String[] args) {List<PersonDTO> list = getDataList();for (int i = 0; i < list.size(); i++) {PersonDTO personDTO = list.get(i);list.remove(personDTO);}System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null)]


普通for循环遍历时新增元素,size若不固定,报异常OutOfMemoryError

    public static void main(String[] args) {List<PersonDTO> 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) {List<PersonDTO> 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(bookEntityList=null, personName=xiaohua1, age=null), PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua3, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua5, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null), PersonDTO(bookEntityList=null, personName=xiaohua1, age=null), PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua3, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua5, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null)]

3.2 增强for循环

增强for循环,遍历元素时重新赋值失败

    public static void main(String[] args) {List<PersonDTO> list = getDataList();for (PersonDTO dto : list) {// 增强for循环内部实现是迭代器,我认为是调用了新方法,进行了值传递,dto是另一个变量了dto = null;}System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=null), PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua3, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua5, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null)]


增强for循环,遍历元素时,对元素中的属性成功赋值

    public static void main(String[] args) {List<PersonDTO> list = getDataList();for (PersonDTO dto : list) {dto.setAge(7);}System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=7), PersonDTO(bookEntityList=null, personName=xiaohua2, age=7), PersonDTO(bookEntityList=null, personName=xiaohua3, age=7), PersonDTO(bookEntityList=null, personName=xiaohua4, age=7), PersonDTO(bookEntityList=null, personName=xiaohua5, age=7), PersonDTO(bookEntityList=null, personName=xiaohua6, age=7)]


增强for循环遍历时删除元素,报异常ConcurrentModificationException

    public static void main(String[] args) {List<PersonDTO> 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) {List<PersonDTO> 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) {List<PersonDTO> list = getDataList();// forEach循环内部实现是匿名内部类,调用了函数式接口的新方法,进行了值传递,dto是另一个变量了list.forEach(dto -> dto = null);System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=null), PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua3, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua5, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null)]


forEach循环,遍历元素时,对元素中的属性成功赋值

    public static void main(String[] args) {List<PersonDTO> list = getDataList();list.forEach(dto -> dto.setAge(6));System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=6), PersonDTO(bookEntityList=null, personName=xiaohua2, age=6), PersonDTO(bookEntityList=null, personName=xiaohua3, age=6), PersonDTO(bookEntityList=null, personName=xiaohua4, age=6), PersonDTO(bookEntityList=null, personName=xiaohua5, age=6), PersonDTO(bookEntityList=null, personName=xiaohua6, age=6)]


forEach循环遍历时删除元素,报异常ConcurrentModificationException

    public static void main(String[] args) {List<PersonDTO> 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) {List<PersonDTO> 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) {List<PersonDTO> list = getDataList();// stream forEach循环内部实现是匿名内部类,调用了函数式接口的新方法,进行了值传递,dto是另一个变量了list.stream().forEach(dto -> dto = null);System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=null), PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua3, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua5, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null)]


stream forEach循环,遍历元素时,对元素中的属性成功赋值

    public static void main(String[] args) {List<PersonDTO> list = getDataList();list.stream().forEach(dto -> dto.setAge(8));System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=8), PersonDTO(bookEntityList=null, personName=xiaohua2, age=8), PersonDTO(bookEntityList=null, personName=xiaohua3, age=8), PersonDTO(bookEntityList=null, personName=xiaohua4, age=8), PersonDTO(bookEntityList=null, personName=xiaohua5, age=8), PersonDTO(bookEntityList=null, personName=xiaohua6, age=8)]


stream forEach循环遍历时删除元素,报异常ConcurrentModificationException

    public static void main(String[] args) {List<PersonDTO> 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) {List<PersonDTO> 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) {List<PersonDTO> list = getDataList();Iterator<PersonDTO> iterator = list.iterator();while (iterator.hasNext()) {// 原因是这里dto是另一个栈变量,并不会对集合中的栈内容(对象在内存中的地址)进行改变PersonDTO dto = iterator.next();dto = null;}System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=null), PersonDTO(bookEntityList=null, personName=xiaohua2, age=null), PersonDTO(bookEntityList=null, personName=xiaohua3, age=null), PersonDTO(bookEntityList=null, personName=xiaohua4, age=null), PersonDTO(bookEntityList=null, personName=xiaohua5, age=null), PersonDTO(bookEntityList=null, personName=xiaohua6, age=null)]


迭代器循环,遍历元素时,对元素中的属性成功赋值

    public static void main(String[] args) {List<PersonDTO> list = getDataList();Iterator<PersonDTO> iterator = list.iterator();while (iterator.hasNext()) {PersonDTO dto = iterator.next();dto.setAge(9);}System.out.println(list);}

打印结果:
[PersonDTO(bookEntityList=null, personName=xiaohua1, age=9), PersonDTO(bookEntityList=null, personName=xiaohua2, age=9), PersonDTO(bookEntityList=null, personName=xiaohua3, age=9), PersonDTO(bookEntityList=null, personName=xiaohua4, age=9), PersonDTO(bookEntityList=null, personName=xiaohua5, age=9), PersonDTO(bookEntityList=null, personName=xiaohua6, age=9)]


迭代器遍历时删除元素,可以成功

    public static void main(String[] args) {List<PersonDTO> list = getDataList();Iterator<PersonDTO> iterator = list.iterator();while (iterator.hasNext()) {PersonDTO dto = iterator.next();iterator.remove();}System.out.println(list);}

打印结果:
[]


迭代器遍历时新增元素,报异常:ConcurrentModificationException

    public static void main(String[] args) {List<PersonDTO> list = getDataList();Iterator<PersonDTO> 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接下来还可能会继续扩展List<PersonDTO> 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(List<PersonDTO> list) {// isNotEnd 代表list集合可能还会增加元素deleteSubList(list, true);}private static void deleteSubList(List<PersonDTO> 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)) {List<PersonDTO> subList = Lists.partition(list, SUBLIST_SIZE).get(0);// 不再继续处理业务逻辑: list中的数据量小于SUBLIST_SIZE && list中还可能增加元素if ((list.size() < SUBLIST_SIZE) && isNotEnd) {break;}// 使用subList处理业务逻辑// 删出list中的subListIterator<PersonDTO> 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 List<PersonDTO> getDataList() {List<PersonDTO> 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

这篇关于Java ArrayList在遍历时删除元素的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/688902

相关文章

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B