javaSe-hashMap
Posted 小拽A
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaSe-hashMap相关的知识,希望对你有一定的参考价值。
package com.java.chap08.sec05;
public class Student {
private String name;
private Integer age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
package com.java.chap08.sec05;
import java.util.HashMap;
import java.util.Iterator;
public class TestHashMap {
public static void main(String[] args) {
HashMap<String,Student> hashMap=new HashMap<String,Student>();
hashMap.put("1号", new Student("张三",10));
hashMap.put("2号", new Student("李四",11));
hashMap.put("3号", new Student("王五",12));
Student s=hashMap.get("1号");
System.out.println(s);
Iterator<String> it=hashMap.keySet().iterator(); // 获取key的集合 ,再获取迭代器
while(it.hasNext()){
String key=it.next(); // 获取key
Student student=hashMap.get(key); // 获取值
System.out.println("key="+key+",value="+student);
}
}
}
}
以上是关于javaSe-hashMap的主要内容,如果未能解决你的问题,请参考以下文章