您的位置:首页 > 移动开发 > 微信开发

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

2016-07-25 17:25 591 查看
//将键值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());

}
}
}

输出如下图:





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: