java集合(List集合与Map集合的数据转换)

List集合与Map集合的数据转换  

  实现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

        4)将Map中每个Entry信息放入List对象

一,创建学生对象

 1 public class Student_3 {
 2 /**
 3  * 使用构造方法Student(int id,String name,int age,String sex )
 4  */
 5     private int id;
 6     private String name;
 7     private int age;
 8     private String sex;
 9
10     public Student_3() {
11     }
12
13     public Student_3(int id, String name, int age, String sex) {
14         this.id = id;
15         this.name = name;
16         this.age = age;
17         this.sex = sex;
18     }
19
20     public int getId() {
21         return id;
22     }
23
24     public String getName() {
25         return name;
26     }
27
28     public int getAge() {
29         return age;
30     }
31
32     public String getSex() {
33         return sex;
34     }
35
36     public void setId(int id) {
37         this.id = id;
38     }
39
40     public void setName(String name) {
41         this.name = name;
42     }
43
44     public void setAge(int age) {
45         this.age = age;
46     }
47
48     public void setSex(String sex) {
49         this.sex = sex;
50     }
51
52     @Override
53     public String toString() {
54         return "["+id +" "+ name +" "+ age +" "+ sex+"]";
55     }
56
57
58 }

  二,创建测试类

 1 import java.util.ArrayList;
 2 import java.util.HashMap;
 3 import java.util.Iterator;
 4 import java.util.List;
 5 import java.util.Map;
 6 import java.util.Map.Entry;
 7 import java.util.Set;
 8
 9
10 public class Test_3 {
11     //List集合转Map集合
12     public void listToMap(Student_3 s1,Student_3 s2 ){
13         //1.创建List集合
14         List<Student_3> list = new ArrayList<>();
15         //2.创建Map集合
16         Map<Integer, Student_3> map = new HashMap<>();
17         //3.传入学生对象
18         list.add(s1);
19         list.add(s2);
20         Iterator<Student_3> it = list.iterator();
21         //4.遍历List集合
22         while(it.hasNext()){
23             Student_3 stu = it.next();
24             //5.将学生id作为Map集合的Key,学生对象作为Map集合的Vaul
25             map.put(stu.getId(), stu);
26         }
27         //6.遍历Map集合
28         Set<Entry<Integer, Student_3>> entrySet = map.entrySet();
29         for (Entry<Integer, Student_3> en : entrySet) {
30             System.out.println(en);
31         }
32
33     }
34
35     //Map集合转List集合
36     public void mapToList(Student_3 s3,Student_3 s4){
37         //1.创建Map集合
38         Map< Integer, Student_3> map = new HashMap<>();
39         //2.创建List集合
40         List<Student_3> list = new ArrayList<>();
41         //3.将学生id作为Map集合的Key,学生对象作为Map集合的Vaul
42         map.put(s3.getId(),s3);
43         map.put(s4.getId(),s4);
44         //4.遍历Map集合
45         Set<Entry<Integer, Student_3>> entrySet = map.entrySet();
46         for (Entry<Integer, Student_3> e : entrySet) {
47             //5.将Map集合的值加入到Liat集合中
48             list.add(e.getValue());
49         }
50         //6.遍历List集合
51         Iterator<Student_3> it = list.iterator();
52         while(it.hasNext()){
53             System.out.println(it.next());
54         }
55
56     }
57     public static void main(String[] args) {
58         //创建四个学生对象
59         Student_3 s1 = new Student_3(101, "张三", 25, "男");
60         Student_3 s2 = new Student_3(102, "马琴", 25, "女");
61         Student_3 s3 = new Student_3(103, "赵薇", 36, "女");
62         Student_3 s4 = new Student_3(104, "李小梅", 31, "女");
63         //创建Test_3的实例
64         Test_3 t = new Test_3();
65         System.out.println("List集合转Map集合");
66         //调用List集合转Map集合方法并传入学生对象s1,s2
67         t.listToMap(s1,s2);
68         System.out.println("\n");
69         System.out.println("Map集合转List集合");
70         //调用Map集合转List集合方法并传入学生对象s3,s4
71         t.mapToList(s3, s4);
72     }
73 }

原文地址:https://www.cnblogs.com/topshark/p/10246765.html

时间: 2024-08-29 09:28:37

java集合(List集合与Map集合的数据转换)的相关文章

黑马程序员——Java基础---集合(二)------Map集合

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ------- Map集合 一.概述 1.简述: Map<K,V>集合是一个接口,和List集合及Set集合不同的是,它是双列集合,并且可以给对象加上名字,即键(Key) 2.特点: 1)该集合存储键值对,一对一对往里存 2)要保证键的唯一性

黑马程序员-----集合框架类(三) Map集合

黑马程序员-----集合框架类(三) Map集合 1.1 Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 1,添加. put(K key, V value) putAll(Map<? extends K,? extends V> m) 2,删除. clear() remove(Object key) 3,判断. containsValue(Object value) containsKey(Object key) isEmpty() 4,获取. get(Object ke

Java集合类学习笔记(Map集合)

Map用于保存具有映射关系的数据,因此Map集合里保存着两组数据,一组用于保存Map的key,一组用于保存key所对应的value. Map的key不允许重复. HashMap和Hashtable都是Map接口的典型实现类,他们的关系类似于ArrayList和Vector的关系. HashMap和Hashtable的区别: Hashtable是一个线程安全的Map实现,但HashMap是线程不安全的实现. Hashtable不允许使用null作为key和value,HashMap可以使用. Li

泛型,JDK5新特性,List集合子实现类,Map集合,Set/TreeSet集合,asList

一.泛型(JDK5以后新特性) 1.概述:泛型直接规定集合的存储类型,将明确的集合类型的工作推迟到了创建对象或者调用方法的时候,属于一种参数化类型,可作参数传递.2.优点(1)将运行时期异常提前到了编译时期:(2)优化了设计,解决了×××警告线问题:(3)避免了强制类型转换, ,解决了向下类型转换出现的问题ClassCastException:(4)泛型的引出可以提供程序的安全性.3.泛型定义在类上(1)格式:public class 类名<T>{--}(2)实例:实体类: 测试类: 4.泛型

struts2获取表单数据之 属性封装 模型驱动 表达式封装 对象封装到list集合 对象封装到map集合 五种方便的封装方式

一.属性封装 属性封装是在action里面设定属性值,属性名字一定要和表单中的name一样,action中extends ActionSupport demo1.jsp: <body> This is my login JSP page. <br> <form action="demo1.action" method="post" > username<input type="text" name=&qu

java集合练习:创建Map集合,创建Emp对象,并将创建的Emp对象添加到集合中

package com.jihe; public class Emp { private String e_id; private String e_name; public Emp(String e_id, String e_name) { super(); this.e_id = e_id; this.e_name = e_name; } public String getE_id() { return e_id; } public void setE_id(String e_id) { t

java集合(四)Map集合之EnumMap详解

一.EnumMap 概述 EnumMap 是一个用于存储 key 为枚举类型的 map,底层使用数组实现(K,V 双数组).下面是其继承结构: public class EnumMap<K extends Enum<K>, V> extends AbstractMap<K, V> implements java.io.Serializable, Cloneable 从上面的继承结构上可以看出 EnumMap 的 key 必须是一个枚举类型,而 value 没有限制. 1

(特别篇)java集合(四)Map集合之WeakHashMap详解之弱引用详解

本篇文章尝试从What.Why.How这三个角度来探索Java中的弱引用,理解Java中弱引用的定义.基本使用场景和使用方法.由于个人水平有限,叙述中难免存在不准确或是不清晰的地方,希望大家可以指出,谢谢大家:) 1. What——什么是弱引用?** Java中的弱引用具体指的是java.lang.ref.WeakReference<T>类,我们首先来看一下官方文档对它做的说明: 弱引用对象的存在不会阻止它所指向的对象变被垃圾回收器回收.弱引用最常见的用途是实现规范映射(canonicaliz

Java遍历Map集合方法

package testMap; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /**  * 循环遍历Map集合  *   * @author Administrator  *   */ pub

Android(java)学习笔记102:Map集合功能概述

下面通过代码引入Map集合:如下 1 package cn.itcast_01; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 /* 7 * 作为学生来说,是根据学号来区分不同的学生的,那么假设我现在已经知道了学生的学号,我要根据学号去获取学生姓名,请问怎么做呢? 8 * 如果采用前面讲解过的集合,我们只能把学号和学生姓名作为一个对象的成员,然后存储整个对象,将来遍历的时候,判断,获取对应的名称. 9 * 但是呢,如果我都能把