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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2660636.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

116基于matlab的盲源信号分离

基于matlab的盲源信号分离。FASTICA方法&#xff0c;能够很好的将信号解混&#xff0c;可以替换数据进行分析。具有GUI界面&#xff0c;可以很好的进行操作。程序已调通&#xff0c;可直接运行。 116matlab盲源信号分离FASTICA (xiaohongshu.com)

20231228在Firefly的AIO-3399J开发板的Android11使用Firefly的DTS配置单前后摄像头ov13850

20231228在Firefly的AIO-3399J开发板的Android11使用Firefly的DTS配置单前后摄像头ov13850 2023/12/28 19:20 缘起&#xff0c;突然发现只能打开前置的ov13850&#xff0c;或者后置的ov13850。 但是不能切换&#xff01; 【SDK&#xff1a;rk3399-android-11-r20211216.tar.xz】…

设备健康管理系统助力制造企业实现数字化转型

在当今快速变革的制造业环境中&#xff0c;数字化转型已成为制造企业保持竞争力和实现可持续发展的关键。在这个数字化转型的浪潮中&#xff0c;设备健康管理系统正发挥着重要的作用。设备健康管理系统通过实时监测、预测分析和智能诊断等功能&#xff0c;为制造企业提供了全面…

Flink实时电商数仓之DWS层

需求分析 关键词 统计关键词出现的频率 IK分词 进行分词需要引入IK分词器&#xff0c;使用它时需要引入相关的依赖。它能够将搜索的关键字按照日常的使用习惯进行拆分。比如将苹果iphone 手机&#xff0c;拆分为苹果&#xff0c;iphone, 手机。 <dependency><grou…

Kubernetes 学习总结(41)—— 云原生容器网络详解

背景 随着网络技术的发展&#xff0c;网络的虚拟化程度越来越高&#xff0c;特别是云原生网络&#xff0c;叠加了物理网络、虚机网络和容器网络&#xff0c;数据包在网络 OSI 七层网络模型、TCP/IP 五层网络模型的不同网络层进行封包、转发和解包。网络数据包跨主机网络、容器…

开箱即用的企业级数据和业务管理中后台前端框架Ant Design Pro 5的开箱使用和偏好配置

Ant Design Pro 介绍 Ant Design Pro 是一个开箱即用的企业级前端解决方案&#xff0c;基于 Ant Design 设计体系&#xff0c;提供了丰富的组件和功能&#xff0c;帮助开发者更快速地开发和部署企业级应用。 Ant Design Pro 使用 React、umi 和 dva 这三个主要的前端开发技术…

elementui+vue2 input输入框限制只能输入数字

方法1 自定义表单校验 <el-form :model"Formdata" ref"formRef" :rules"nodeFormRules" label-width"100px"><el-form-itemlabel"年龄"prop"age"><el-input v-model.number"Formdata.age&q…

HackTheBox-Machines--Photobomb

文章目录 1 端口扫描2 测试思路3 Web漏洞探测4 权限提升 Photobomb 测试过程 1 端口扫描 nmap -sC -sV 10.129.57.2102 测试思路 目标开启了22、80端口&#xff0c;所以测试点还是从80端口开始。 针对80端口的测试&#xff1a;   1.目录扫描   2.网页源代码   3.web漏洞 …

Java开发框架和中间件面试题(10)

目录 104.怎么保证缓存和数据库数据的一致性&#xff1f; 105.什么是缓存穿透&#xff0c;什么是缓存雪崩&#xff1f;怎么解决&#xff1f; 106.如何对数据库进行优化&#xff1f; 107.使用索引时有哪些原则&#xff1f; 108.存储过程如何进行优化&#xff1f; 109.说说…

白话机器学习的数学-1-回归

1、设置问题 投入的广告费越多&#xff0c;广告的点击量就越高&#xff0c;进而带来访问数的增加。 2、定义模型 定义一个函数&#xff1a;一次函数 y ax b &#xff08;a 是斜率、b 是截距&#xff09; 定义函数&#xff1a; 3、最小二乘法 例子&#xff1a; 用随便确定的参…

node 项目中 __dirname / __filename 是什么,为什么有时候不能用?

__dirname 是 Node.js 中的一个特殊变量&#xff0c;表示当前执行脚本所在的目录的绝对路径。 __filename 同理&#xff0c;是 Node.js 中的一个特殊变量&#xff0c;表示当前执行脚本的绝对路径&#xff0c;包括文件名。 在 Node.js 中&#xff0c;__dirname / __filename是…

用通俗易懂的方式讲解大模型:Prompt 提示词在开发中的使用

OpenAI 的 ChatGPT 是一种领先的人工智能模型&#xff0c;它以其出色的语言理解和生成能力&#xff0c;为我们提供了一种全新的与机器交流的方式。但不是每个问题都可以得到令人满意的答案&#xff0c;如果想得到你所要的回答就要构建好你的提示词 Prompt。本文将探讨 Prompt 提…

鸿鹄电子招投标系统:基于Spring Boot、Mybatis、Redis和Layui的企业电子招采平台源码与立项流程

在数字化时代&#xff0c;企业需要借助先进的数字化技术来提高工程管理效率和质量。招投标管理系统作为企业内部业务项目管理的重要应用平台&#xff0c;涵盖了门户管理、立项管理、采购项目管理、采购公告管理、考核管理、报表管理、评审管理、企业管理、采购管理和系统管理等…

elasticsearch系列九:异地容灾-CCR跨集群复制

概述 起初只在部分业务中采用es存储数据&#xff0c;在主中心搭建了个集群&#xff0c;随着es在我们系统中的地位越来越重要&#xff0c;数据也越来越多&#xff0c;针对它的安全性问题也越发重要&#xff0c;那如何对es做异地容灾呢&#xff1f; 今天咱们就一起看下官方提供的…

分布式事务之最终一致性

分布式事务之最终一致性 参考链接分布式事务基础理论概述案例解决方案:RocketMQ可靠消息注意事项&#xff1a;代码实现 参考链接 原文链接&#xff1a;https://blog.csdn.net/jikeyeka/article/details/126296938 分布式事务基础理论 基于上述的CAP和BASE理论,一般情况下会保…

西北大学844计算机类考研-25级初试高分总攻略

西北大学844计算机类考研-25级初试高分攻略 个人介绍 ​ 本人是西北大学22级软件工程研究生&#xff0c;考研专业课129分&#xff0c;过去一年里在各大辅导机构任职&#xff0c;辅导考研学生专业课844&#xff0c;辅导总时长达400小时&#xff0c;辅导学生超过20余人&#xf…

展现无限创意的Photoshop 2023 Mac/win中文版:打造您的独特艺术之旅

无论您是摄影师、设计师还是艺术家&#xff0c;Photoshop 2023&#xff08;ps 2023&#xff09;都是您不可或缺的创意工具。最新升级的Photoshop 2023带来了更多令人兴奋的功能和改进&#xff0c;让您能够以前所未有的方式展现无限创意。 首先&#xff0c;Photoshop 2023拥有强…

uni-app引入vant表单(附源码)

新建项目 下载安装vant npm i vant main.js引入 import { Form } from vant; import { Field } from vant;Vue.use(Form); Vue.use(Field);代码引入 <van-form submit"onSubmit"><van-fieldclass"rePwd"v-model"username"name"请…

SpringBoot 接口对数据枚举类型的入参以及出参转换处理

目录 1、在项目中使用枚举类型2、不做任何处理的演示效果2.1、接口出参2.2、接口入参 3、用枚举的code作为参数和返回值3.1 代码案例3.1.1、定义枚举基础接口BaseEnum&#xff0c;每个枚举都实现该接口3.1.2、性别Sex枚举并实现接口BaseEnum3.1.3、定义BaseEnum枚举接口序列化3…

Python+OpenCV 零基础学习笔记(4-5):计算机图形基础+Python相对文件路径+OpenCV图像+OpenCV视频

文章目录 相关链接运行环境前言计算机图形OpenCV简单使用图形读取文件读取可能会出现的问题&#xff1a;路径不对解决方案其它路径问题解决方案 图像显示保存OpenCV视频视频素材如何获取&#xff1f;简单视频读取 相关链接 【2022B站最好的OpenCV课程推荐】OpenCV从入门到实战 …