实现List和Map数据的转换。具体要求如下:

功能1:定义方法public void listToMap( ){ }将List中Student元素封装到Map中
1)使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List
2)遍历List,输出每个Student信息
3)将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value
4)遍历Map,输出每个Entry的key和value

功能2:定义方法public void mapToList( ){ }将Map中Student映射信息封装到List
1)创建实体类StudentEntry,可以存储Map中每个Entry的信息
2)使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息,并使用Student的id属性作为key,存入Map
3)创建List对象,每个元素类型是StudentEntry
将Map中每个Entry信息放入List对象

List集合转Map集合:Student类和Test_Student类
Student类:

package Day610.Practice.Demo_ListAndMap;
public class Student {private int id;private String name;private int age;private String sex;public Student() {}public Student(int id, String name, int age, String sex) {this.id = id;this.name = name;this.age = age;this.sex = sex;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public void mapToList(){}
}

Test_Student类

package Day610.Practice.Demo_ListAndMap;import java.util.*;public class Test_Student {public static void main(String[] args) {listToMap();//调用方法}public static void listToMap(){Student s1 = new Student(1001, "zs", 22, "男");Student s2 = new Student(1002, "ls", 23, "女");Student s3 = new Student(1003, "ww", 21, "女");Student s4 = new Student(1004, "zl", 20, "男");Student s5 = new Student(1005, "eh", 24, "男");List<Student> list = new ArrayList<>();//多态创建List对象Map<Integer,Student> map = new HashMap<>();//多态创建Map对象list.add(s1);list.add(s2);list.add(s3);list.add(s4);list.add(s5);//遍历ListSystem.out.println("List集合输出");for (Student ss1 : list) {//增强forSystem.out.println(ss1.getId()+","+ss1.getName()+","+ss1.getAge()+","+ss1.getSex());//遍历一次List集合,就添加一次到Map集合中map.put(ss1.getId(),ss1);}System.out.println("Map集合输出");Set<Map.Entry<Integer, Student>> entries = map.entrySet();//Map中的entrySet方法获取所有的K,V,放在Set集合中for (Map.Entry<Integer, Student> m:entries){//增强forSystem.out.println(m.getKey()+","+m.getValue().getName()+","+m.getValue().getAge()+","+m.getValue().getSex());}}
}

以上是List集合转Map集合

下面是Map集合转List集合
StudentEntry类:

package Day610.Practice.Demo_ListAndMap;
public class StudentEntry {private int id;private String name;private int age;private String sex;public StudentEntry() {}public StudentEntry(int id, String name, int age, String sex) {this.id = id;this.name = name;this.age = age;this.sex = sex;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}

Test_StudentEntry类

package Day610.Practice.Demo_ListAndMap;import java.util.*;public class Test_StudentEntry {public static void main(String[] args) {mapToList();}public static void mapToList(){StudentEntry s1 = new StudentEntry(1001, "zs", 22, "男");StudentEntry s2 = new StudentEntry(1002, "ls", 23, "女");StudentEntry s3 = new StudentEntry(1003, "ww", 21, "女");StudentEntry s4 = new StudentEntry(1004, "zl", 20, "男");StudentEntry s5 = new StudentEntry(1005, "eh", 24, "男");Map<Integer,StudentEntry> map = new HashMap<>();//多态创建Map对象map.put(s1.getId(),s1);map.put(s2.getId(),s2);map.put(s3.getId(),s3);map.put(s4.getId(),s4);map.put(s5.getId(),s5);List<StudentEntry> list = new ArrayList<>();//多态创建List对象Set<Map.Entry<Integer, StudentEntry>> entries = map.entrySet();//Map中的entrySet方法获取所有的K,V,放在Set集合中System.out.println("Map集合输出");for (Map.Entry<Integer, StudentEntry> m:entries){//增强forSystem.out.println(m.getKey()+","+m.getValue().getName()+","+m.getValue().getAge()+","+m.getValue().getSex());//遍历一次map集合,就在List集合中添加一次list.add(m.getValue());}System.out.println("List集合输出");for (StudentEntry s:list){//增强forSystem.out.println(s.getId()+","+s.getName()+","+s.getAge()+","+s.getSex());}}
}

List和Map的互相转化相关推荐

  1. json与javabean、list、map之间的转化

    一.java普通对象和json字符串的互转 java对象---->json 首先创建一个java对象: public class Student {//姓名private String name ...

  2. Map的value转化为其它类型

    map的values() Collection<String> v = map.values(); 转化为数组或者List集合 转化为List: List<String> l= ...

  3. map geometry wkt转化

    wkt---geometry           wkt中有Z的在转化成geometry的时候会保留z坐标      "POLYGONZ((120.5521 60.6667,121.5521 ...

  4. SQL老司机,在SQL中计算 array map json数据

    2019独角兽企业重金招聘Python工程师标准>>> 摘要: 场景 通常,我们处理数据,一列数据类型要么是字符串,要么是数字,这些都是primitive类型的数据. 场景 通常,我 ...

  5. 数组 spark_spark——rdd常用的转化和行动操作

    今天是spark第三篇文章,我们继续来看RDD的一些操作. 我们前文说道在spark当中RDD的操作可以分为两种,一种是转化操作(transformation),另一种是行动操作(action).在转 ...

  6. Guava学习之Map

    Guava 中文是石榴的意思,该项目是 Google 的一个开源项目,包含许多 Google 核心的 Java 常用库. 目前主要包含: com.google.common.annotations c ...

  7. spark convert RDD[Map] to DataFrame

    将RDD[Map[String,String]] 转化为展平 DataFrame,类似于pyspark 中 dict 结构toDF的效果. input val mapRDD: RDD[Map[Stri ...

  8. ES6:连女朋友看了都喜欢的小知识-如何排序一个Map对象

    大家好,我是林三心,众所周知(不要问我谁是"众所"),Map 是 ES6 中新增的数据结构,Map 类似于对象,但普通对象的 key 必须是字符串或者数字,而 Map 的 key ...

  9. java map 递归_Java实现递归将嵌套Map里的字段名由驼峰转为下划线

    摘要: 使用Java语言递归地将Map里的字段名由驼峰转下划线.通过此例可以学习如何递归地解析任意嵌套的List-Map容器结构. 难度:初级 概述### 在进行多语言混合编程时,由于编程规范的不同, ...

最新文章

  1. Flutter UI自动化测试技术方案选型与探索
  2. ViewPager实现翻页步骤
  3. 使用Pylint进行Python代码规范检查
  4. java基础英语---第二十一天
  5. 2021绵阳南山高考成绩查询,绵阳南山中学举行2021年冲刺高考百日誓师大会
  6. Vue项目中使用 路由导航守卫 处理页面的访问权限
  7. python数据库sqlite3_Python 操作 SQLite 数据库
  8. 天天酷跑php源码_使用Java实现天天酷跑(附源码)
  9. 最近围绕生鲜社区团购的一些事
  10. Spark SQL External DataSource外部数据源操作流程
  11. 【炼丹技巧】惊了,掌握了这个炼丹技巧的我开始突飞猛进
  12. 2020-12-29 zabbix 安装 snmpwalk 命令和基本使用
  13. GD32F103ZKT6替换STM32F103调试手记
  14. 51 TMOD、TCON设置定时
  15. Netty实战《原理》
  16. 微信公众号服务号怎么添加模板消息给所有粉丝群发
  17. 防火门行业研究及十四五规划分析报告
  18. 解决File.Delete()删除不掉文件
  19. 大数据开发,想写代码不给我写,到底是人性的扭曲,还是道德的沦丧?
  20. [费用流] LOJ#545. 「LibreOJ β Round #7」小埋与游乐场

热门文章

  1. 使用SQL批量处理文章中的图片路径
  2. Linux用户(user)和用户组(group)的日常管理与操作
  3. 2018-2019-2 20189206 《网络攻防实践》 第十周作业
  4. LSP到底有什么用?
  5. prometheus 企业监控常用函数,与简单的实际应用分析
  6. 转载 硬核图解!30张图带你搞懂!路由器,集线器,交换机,网桥,光猫有啥区别?
  7. 基于ssm的流浪猫狗救助系统(springmvc spring mybatis mysql jsp)1002
  8. css样式的三种引入方式
  9. 问题随记 —— 在 edge 上看视频会绿屏
  10. Navicat提示Access violation at address in module ‘navicate.exe‘. Read of address 0000000000000000