关于java中map存储多行的小程序(一键对多值)

来源:互联网 发布:阿里云视频直播php 编辑:程序博客网 时间:2024/06/01 10:48
//将键值value设置为Student对象,Student对象可自行设置,代码如下:
public class Student {private int id;private String name;private int score;public void setId(int id){this.id=id;}public void setName(String name){this.name=name;}public void setScore(int score){this.score=score;}public Integer getId(){return id;}public String getName(){return name;}public Integer getScore(){return score;}}
</pre><p>类mapTest2:</p><pre name="code" class="java">package practice;import java.util.*;import java.util.Map.Entry;public class mapTest2 {public static void main(String[] args) {Student obj1=new Student();obj1.setId(1);obj1.setName("xl");obj1.setScore(99);Student obj2=new Student();obj2.setId(2);obj2.setName("cq");obj2.setScore(98);Student obj3=new Student();obj3.setId(3);obj3.setName("xh");obj3.setScore(97);HashMap<String, Student> map = new HashMap<String, Student>();map.put("1", obj1);  map.put("2", obj2);map.put("3", obj3);List<Map<String, Student>> list = new ArrayList<Map<String, Student>>();  list.add(map); for (Entry<String, Student> entry : map.entrySet()) {    if(entry.getValue().getScore()==97){    Student obj=entry.getValue();    obj.setScore(100);    String i=entry.getKey();    map.put(i, obj);    System.out.println("Key = " + entry.getKey() + ",学号 = " + entry.getValue().getId()+ ",姓名 = "+ entry.getValue().getName()+",成绩 = "+entry.getValue().getScore());    }    else  System.out.println("Key = " + entry.getKey() + ",学号 = " + entry.getValue().getId()+ ",姓名 = "+ entry.getValue().getName()+",成绩 = "+entry.getValue().getScore());            }  }}

输出如下图:


                                             
0 1
原创粉丝点击