Map.Entry与entrySet与entry,getKey()与entry.getValue()的用法

2024-03-11 03:08

本文主要是介绍Map.Entry与entrySet与entry,getKey()与entry.getValue()的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接上代码 实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SinglePressureResultDTO {private Integer Times;   private Integer SCU_number; private Boolean Intervention;  private Long startTime_low;   private Long low_time;   private Long startTime_rise;  private Long endTime_rise;  private Long rise_time;   private Long total_time;  private Long endTime_resistance; private Long resistance_time;   private Long time_resistance_turningup;  private Float pressure_resistance_turningup;  private Float end_resistance;   private Float initial_support;private Float moving_pressure;
}

测试

public static void main(String[] args) {//给集合赋值  元素为SinglePressureResultDTO对象List<SinglePressureResultDTO> singlePressure=new ArrayList<>();singlePressure.add(new SinglePressureResultDTO(1,2,true,1l,1l,1l,1l,1l,1l,1l,1l,1l,1f,1f,1f,1f));singlePressure.add(new SinglePressureResultDTO(1,3,true,1l,1l,1l,1l,1l,1l,1l,1l,1l,1f,1f,1f,1f));        singlePressure.add(new SinglePressureResultDTO(1,4,true,2l,1l,1l,1l,1l,1l,1l,1l,1l,1f,1f,1f,1f));singlePressure.add(new SinglePressureResultDTO(1,4,false,3l,1l,1l,1l,1l,1l,1l,1l,1l,1f,1f,1f,1f));System.out.println(adapterToASQTotalDTO(singlePressure)+"   )))");public static ASQTotalDTO adapterToASQTotalDTO(List<SinglePressureResultDTO> singlePressure) {ASQTotalDTO asqTotalDTO = new ASQTotalDTO();List<ASQSingleDTO> asqs = new ArrayList<>();if (CollectionUtil.isEmpty(singlePressure)) {return asqTotalDTO;}//将传递过来的集合以SCU_number属性进行分组  类似于key-value的形式  //key是SCU_number,value是SCU_number符合条件的所有对象(集合)//{2=[SinglePressureResultDTO(Times=1, SCU_number=2, Intervention=true, startTime_low=1, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0)],// 3=[SinglePressureResultDTO(Times=1, SCU_number=3, Intervention=true, startTime_low=1, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0)],// 4=[SinglePressureResultDTO(Times=1, SCU_number=4, Intervention=true, startTime_low=2, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0), SinglePressureResultDTO(Times=1, SCU_number=4, Intervention=false, startTime_low=3, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0)]}    Map<Integer, List<SinglePressureResultDTO>> scuResult = singlePressure.stream().collect(Collectors.groupingBy(SinglePressureResultDTO::getSCU_number));System.out.println(scuResult+"  scuResult");//获取每一个集合中的元素  确切来说,获取每一个key-value//并且遍历每一个key-valuefor (Map.Entry<Integer, List<SinglePressureResultDTO>> entry : scuResult.entrySet()) {//在这里可以打印每一个key-valueSystem.out.println(entry+"  key-value");ASQSingleDTO asqSingle = new ASQSingleDTO();List<ASQCirculateDTO> auto = new ArrayList<>();List<ASQCirculateDTO> manual = new ArrayList<>();//遍历每一个key-value中的value(注意 value是集合)for (SinglePressureResultDTO pressure : entry.getValue()) {//同样可以获取到entry中的key以及entry中的valueSystem.out.println(entry.getKey()+"  **&^&%^");System.out.println(entry.getValue()+"   *&^%");ASQCirculateDTO dto = new ASQCirculateDTO();dto.setDownStartTime(pressure.getStartTime_low());dto.setDownEndTime(pressure.getStartTime_rise());dto.setUpStartTime(pressure.getStartTime_rise());dto.setUpEndTime(pressure.getEndTime_rise());dto.setEndStrength(pressure.getEnd_resistance());dto.setInflectionPressure(pressure.getPressure_resistance_turningup());dto.setInitStrength(pressure.getInitial_support());if (pressure.getIntervention()) {manual.add(dto);} else {auto.add(dto);}}asqSingle.setScuNo(entry.getKey());System.out.println(entry.getKey()+"   entry.getKey()");asqSingle.setAsqNum(auto.size());asqSingle.setNsqNum(manual.size());asqSingle.setAuto(auto);asqSingle.setManual(manual);//每个支架数据作为asqs的一个元素asqs.add(asqSingle);}asqTotalDTO.setAsqs(asqs);System.out.println(asqTotalDTO);List<ASQSingleDTO> asqs1 = asqTotalDTO.getAsqs();//for (ASQSingleDTO asqSingleDTO:asqs1) {//System.out.println(asqSingleDTO+"   -----");//}System.out.println(asqs1+" asq1");return asqTotalDTO;}}

运行 System.out.println(asqs1+" asq1");结果

[ASQSingleDTO{scuNo=2, asqNum=0, nsqNum=1, auto=[], manual=[ASQCirculateDTO{downStartTime=1, downEndTime=1, moveStartTime=null, moveEndTime=null, upStartTime=1, upEndTime=1, inflectionPressure=1.0, initStrength=1.0, endStrength=1.0}]}, 
ASQSingleDTO{scuNo=3, asqNum=0, nsqNum=1, auto=[], manual=[ASQCirculateDTO{downStartTime=1, downEndTime=1, moveStartTime=null, moveEndTime=null, upStartTime=1, upEndTime=1, inflectionPressure=1.0, initStrength=1.0, endStrength=1.0}]}, 
ASQSingleDTO{scuNo=4, asqNum=1, nsqNum=1, auto=[ASQCirculateDTO{downStartTime=3, downEndTime=1, moveStartTime=null, moveEndTime=null, upStartTime=1, upEndTime=1, inflectionPressure=1.0, initStrength=1.0, endStrength=1.0}], manual=[ASQCirculateDTO{downStartTime=2, downEndTime=1, moveStartTime=null, moveEndTime=null, upStartTime=1, upEndTime=1, inflectionPressure=1.0, initStrength=1.0, endStrength=1.0}]}] asq1

打印key-value运行结果

2=[SinglePressureResultDTO(Times=1, SCU_number=2, Intervention=true, startTime_low=1, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0)]  key-value
3=[SinglePressureResultDTO(Times=1, SCU_number=3, Intervention=true, startTime_low=1, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0)]  key-value
4=[SinglePressureResultDTO(Times=1, SCU_number=4, Intervention=true, startTime_low=2, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0), SinglePressureResultDTO(Times=1, SCU_number=4, Intervention=false, startTime_low=3, low_time=1, startTime_rise=1, endTime_rise=1, rise_time=1, total_time=1, endTime_resistance=1, resistance_time=1, time_resistance_turningup=1, pressure_resistance_turningup=1.0, end_resistance=1.0, initial_support=1.0, moving_pressure=1.0)]  key-value

在这里插入图片描述

这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!! 欢迎加入技术群聊。

在这里插入图片描述

这篇关于Map.Entry与entrySet与entry,getKey()与entry.getValue()的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用