赞
踩
遇到的问题:用stream流操作 对象集合
baseinfos
中 对象的属性age level workyears
,用一个辅助集合helper
收集数据,但是结果最终是一样的(如下图);
在Java中,Map的value可以保存数组的引用。当将数组作为value存储在Map中时,实际上是将数组的引用作为value存储,而不是将数组的副本存储。这意味着当你修改数组时,存储在Map中的value也会发生相应的改变。
下面是一个示例代码,演示了如何将数组作为value存储在Map中:
import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { // 创建一个数组 int[] array = {1, 2, 3}; // 创建一个Map Map<String, int[]> map = new HashMap<>(); // 将数组作为value存储在Map中 map.put("array", array); // 修改数组 array[0] = 10; // 输出修改后的数组和Map中保存的数组值 System.out.println("数组: " + Arrays.toString(array)); System.out.println("Map中保存的数组值: " + Arrays.toString(map.get("array"))); } }
输出结果:
数组: [10, 2, 3]
Map中保存的数组值: [10, 2, 3]
从输出结果可以看出,当修改数组时,Map中保存的数组值也发生了相应的改变。
需要注意的是,当通过Map的value修改数组时,必须使用数组的引用来进行操作,而不是通过Map的value直接重新赋值一个新的数组。否则,对数组的修改将不会反映在Map中。
ArrayList<Baseinfo> baseinfos = (ArrayList<Baseinfo>) baseinfoMapper.selectList(null);
HashMap<String, ArrayList<String>> map = new HashMap<>();
ArrayList<String> helper = new ArrayList<>();
baseinfos.stream().forEach((item)->helper.add(item.getAge().toString()));
map.put("age",helper);
helper.clear();
baseinfos.stream().forEach((item)->helper.add(item.getLevel()));
map.put("level",helper);
helper.clear();
baseinfos.stream().forEach((item)->helper.add(item.getWorkyears()));
map.put("workYears",helper);
System.out.println(map.toString());
JDBC Connection [HikariProxyConnection@1652214584 wrapping com.mysql.cj.jdbc.ConnectionImpl@32b95de] will not be managed by Spring ==> Preparing: SELECT id,pid,name,age,level,collage,workyears,alltag,createtime,updatetime,isdelete FROM baseinfo ==> Parameters: <== Columns: id, pid, name, age, level, collage, workyears, alltag, createtime, updatetime, isdelete <== Row: 1, 179, Guo Lan, 47, 4AO0Usq6TK, HokU8wB04S, LzfVBJ9iPC, FJRQCeIPSP, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 2, 247, Lok Fu Shing, 23, mZ3vLbd2WP, wOdzGk7QMw, wK5ubvtqIC, oGwX4qV8ke, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 3, 448, Chang Yun Fat, 38, wY1THGmSey, Z9tOkFcofV, NW2Wr2HEzE, z2rmZzlEjQ, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 4, 450, Chin Wing Fat, 31, 2KyvcWUzAd, cDiekbZPpK, zuMAtpABF6, a4TChHpTX0, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 5, 881, Mark Webb, 33, aNLf9vC6B4, RRQs8MD5si, LXRYXZN87z, ZSHSWk1eeh, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 6, 262, Sugawara Eita, 39, JlJSKEBXN1, 7AkSV4O50i, Nb6DhcQ4vD, 281reUQ88R, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 7, 938, Sugawara Ayano, 40, dH7FRyh0z0, lapYHrIGZW, tvYWvR9g0g, eLXsNmW9AH, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 8, 509, Yam Chi Ming, 25, rBqgzLjwwp, iAHjkrrdWI, s5fLFbgxRw, Vj83RartUU, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 9, 592, Jimmy Morales, 42, H5h4eFzfMC, KsriQCaxkS, su2OREHXoS, be4kMxitpl, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 10, 243, Richard Wood, 23, IP78QVNtpG, FjVgVXkzYQ, mz9ooqLwq3, JyidPt7BqB, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Total: 10 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@71b0289b] {workYears=[LzfVBJ9iPC, wK5ubvtqIC, NW2Wr2HEzE, zuMAtpABF6, LXRYXZN87z, Nb6DhcQ4vD, tvYWvR9g0g, s5fLFbgxRw, su2OREHXoS, mz9ooqLwq3], level=[LzfVBJ9iPC, wK5ubvtqIC, NW2Wr2HEzE, zuMAtpABF6, LXRYXZN87z, Nb6DhcQ4vD, tvYWvR9g0g, s5fLFbgxRw, su2OREHXoS, mz9ooqLwq3], age=[LzfVBJ9iPC, wK5ubvtqIC, NW2Wr2HEzE, zuMAtpABF6, LXRYXZN87z, Nb6DhcQ4vD, tvYWvR9g0g, s5fLFbgxRw, su2OREHXoS, mz9ooqLwq3]}
ArrayList<Baseinfo> baseinfos = (ArrayList<Baseinfo>) baseinfoMapper.selectList(null); HashMap<String, ArrayList<String>> map = new HashMap<>(); ArrayList<String> helper = new ArrayList<>(); baseinfos.forEach((item)->helper.add(item.getAge().toString())); baseinfos.stream().forEach((item)->helper.add(item.getAge().toString())); map.put("age",helper); ArrayList<String> helper1 = new ArrayList<>(); baseinfos.stream().forEach((item)->helper1.add(item.getLevel())); map.put("level",helper1); ArrayList<String> helper2 = new ArrayList<>(); baseinfos.stream().forEach((item)->helper2.add(item.getWorkyears())); map.put("workYears",helper2); System.out.println(map.toString());
JDBC Connection [HikariProxyConnection@1258934723 wrapping com.mysql.cj.jdbc.ConnectionImpl@18811c42] will not be managed by Spring ==> Preparing: SELECT id,pid,name,age,level,collage,workyears,alltag,createtime,updatetime,isdelete FROM baseinfo ==> Parameters: <== Columns: id, pid, name, age, level, collage, workyears, alltag, createtime, updatetime, isdelete <== Row: 1, 179, Guo Lan, 47, 4AO0Usq6TK, HokU8wB04S, LzfVBJ9iPC, FJRQCeIPSP, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 2, 247, Lok Fu Shing, 23, mZ3vLbd2WP, wOdzGk7QMw, wK5ubvtqIC, oGwX4qV8ke, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 3, 448, Chang Yun Fat, 38, wY1THGmSey, Z9tOkFcofV, NW2Wr2HEzE, z2rmZzlEjQ, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 4, 450, Chin Wing Fat, 31, 2KyvcWUzAd, cDiekbZPpK, zuMAtpABF6, a4TChHpTX0, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 5, 881, Mark Webb, 33, aNLf9vC6B4, RRQs8MD5si, LXRYXZN87z, ZSHSWk1eeh, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 6, 262, Sugawara Eita, 39, JlJSKEBXN1, 7AkSV4O50i, Nb6DhcQ4vD, 281reUQ88R, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 7, 938, Sugawara Ayano, 40, dH7FRyh0z0, lapYHrIGZW, tvYWvR9g0g, eLXsNmW9AH, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 8, 509, Yam Chi Ming, 25, rBqgzLjwwp, iAHjkrrdWI, s5fLFbgxRw, Vj83RartUU, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Row: 9, 592, Jimmy Morales, 42, H5h4eFzfMC, KsriQCaxkS, su2OREHXoS, be4kMxitpl, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 1 <== Row: 10, 243, Richard Wood, 23, IP78QVNtpG, FjVgVXkzYQ, mz9ooqLwq3, JyidPt7BqB, 2023-06-19 17:22:50, 2023-06-19 17:22:50, 0 <== Total: 10 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@ae3c140] {workYears=[LzfVBJ9iPC, wK5ubvtqIC, NW2Wr2HEzE, zuMAtpABF6, LXRYXZN87z, Nb6DhcQ4vD, tvYWvR9g0g, s5fLFbgxRw, su2OREHXoS, mz9ooqLwq3], level=[4AO0Usq6TK, mZ3vLbd2WP, wY1THGmSey, 2KyvcWUzAd, aNLf9vC6B4, JlJSKEBXN1, dH7FRyh0z0, rBqgzLjwwp, H5h4eFzfMC, IP78QVNtpG], age=[47, 23, 38, 31, 33, 39, 40, 25, 42, 23, 47, 23, 38, 31, 33, 39, 40, 25, 42, 23]}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。