keySet:
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("01", "张三01");
map.put("02", "张三02");
map.put("03", "张三03");
map.put("04", "张三04");
//先获取map集合的所有键的Set集合,KeySet();
Set<String> keySet = map.keySet();
//有了set集合,就可以获取迭代器。
Iterator <String> it = keySet.iterator();
while(it.hasNext()) {
String key = it.next();
//有了键就可以通过map集合的get方法获取其对应的值。
System.out.println("key="+key);
}
}
}
map练习:
public static void main(String[] args) {
HashMap<Student,String> hm = new HashMap<Student,String>();
hm.put(new Student("lisi1",21), "beijing");
hm.put(new Student("lisi2",22), "shanghai");
hm.put(new Student("lisi3",23), "wuhan");
hm.put(new Student("lisi4",24), "tianjin");
//第一种取出方式
// Set<Student> keySet = hm.keySet();
// Iterator it = keySet.iterator();
Iterator<Student> it = hm.keySet().iterator();
while(it.hasNext()) {
Student stu =it.next();
String addr =hm.get(stu);
System.out.println(stu+"..."+addr);
}
//第二种取出方式
Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
Iterator<Map.Entry<Student, String>> iter = hm.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<Student, String> me =iter.next();
Student su =me.getKey();
String addr =me.getValue();
System.out.println(su+"..........."+addr);
}
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public int hashCold() {
return name.hashCode()+age*34;
}
public boolean equals(Object obj) {
if(!(obj instanceof Student)) {
throw new ClassCastException("类型不匹配");
}
Student s = (Student)obj;
return this.name.equals(s.name) && this.age==s.age;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setAge(int a) {
age = a;
}
public int age() {
return age;
}
public String toString() {
return name+":"+age;
}
public int compareTo(Student s) {
int num = new Integer(this.age).compareTo(new Integer(s.age));
if(num==0) {
return this.name.compareTo(s.name);
}
return num;
}
}
map循环嵌套练习:
public static void main(String[] args) {
demo();
}
public static void demo() {
//学校总集合
HashMap <String,List<Student1212>> hm = new HashMap<String,List<Student1212>>();
//创建两个班级集合
ArrayList<Student1212> yule = new ArrayList<Student1212>();
ArrayList<Student1212> jiuye = new ArrayList<Student1212>();
hm.put("yule", yule);
hm.put("jiuye", jiuye);
yule.add(new Student1212("01","zhangsan"));
yule.add(new Student1212("02","lisi"));
jiuye.add(new Student1212("01","wangwu"));
jiuye.add(new Student1212("01","zhouliu"));
Iterator<String> it = hm.keySet().iterator();
while(it.hasNext()) {
String roomName = it.next();
List<Student1212> room = hm.get(roomName);
System.out.println(roomName);
getInfos(room);
}
}
public static void getInfos(List<Student1212> list) {
Iterator<Student1212> it = list.iterator();
while(it.hasNext()) {
Student1212 s = it.next();
System.out.println(s);
}
}
}
class Student1212{
private String id;
private String name;
Student1212(String id,String name){
this.name=name;
this.id=id;
}
public String toString() {
//覆写输出方法
return id+":::"+name;
}
}