This code demonstrates how to convert a List of objects (ServiceTimesAccountVo) into a List of Maps using both a traditional for loop and Java Stream API. The traditional loop iterates through each object, creating a Map for each object and populating it with data extracted from the object. The Stream API approach uses the map function to transform each object into a Map and then collects the transformed Maps into a new List. Both methods achieve the same outcome, but the Stream API offers a more concise and expressive way to handle data transformations. "final List<Map<String, Object>> sheetData = new ArrayList<>();\n for (final ServiceTimesAccountVo serviceTimesAccountVo : serviceTimesAccountVos) {\n final Map<String, Object> map = new HashMap<>();\n map.put("empCode", serviceTimesAccountVo.getEmployeeNo());\n map.put("empName", serviceTimesAccountVo.getReqName());\n map.put("orgName", serviceTimesAccountVo.getCmpName());\n map.put("deptName", serviceTimesAccountVo.getDeptName());\n map.put("frequency", serviceTimesAccountVo.getFrequency());\n sheetData.add(map);\n }\n\n return sheetData;" "final List<Map<String, Object>> sheetData = serviceTimesAccountVos.stream()\n .map(serviceTimesAccountVo -> {\n Map<String, Object> map = new HashMap<>();\n map.put("empCode", serviceTimesAccountVo.getEmployeeNo());\n map.put("empName", serviceTimesAccountVo.getReqName());\n map.put("orgName", serviceTimesAccountVo.getCmpName());\n map.put("deptName", serviceTimesAccountVo.getDeptName());\n map.put("frequency", serviceTimesAccountVo.getFrequency());\n return map;\n })\n .collect(Collectors.toList());\n\nreturn sheetData;

Java List to Map Conversion Using Stream vs Traditional Loop

原文地址: https://www.cveoy.top/t/topic/qaDA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录