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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

oracle中exists和not exists用法举例详解

《oracle中exists和notexists用法举例详解》:本文主要介绍oracle中exists和notexists用法的相关资料,EXISTS用于检测子查询是否返回任何行,而NOTE... 目录基本概念:举例语法pub_name总结 exists (sql 返回结果集为真)not exists (s

Springboot中Jackson用法详解

《Springboot中Jackson用法详解》Springboot自带默认json解析Jackson,可以在不引入其他json解析包情况下,解析json字段,下面我们就来聊聊Springboot中J... 目录前言Jackson用法将对象解析为json字符串将json解析为对象将json文件转换为json