zoukankan      html  css  js  c++  java
  • 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("
    ");
    69         System.out.println("Map集合转List集合");
    70         //调用Map集合转List集合方法并传入学生对象s3,s4
    71         t.mapToList(s3, s4);
    72     }
    73 }
  • 相关阅读:
    JFreeChart生成图片
    itext生成Word
    itext生成PDF
    物理模型name与comment互相转化
    表单序列化为Json(只限input)
    c#多线程同步之EventWaitHandle的应用
    C#多线程之异步编程
    Java环境变量设置
    sharepoint 2013实践
    WPF研究之道——数据驱动UI
  • 原文地址:https://www.cnblogs.com/topshark/p/10246765.html
Copyright © 2011-2022 走看看