java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息...

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第1张图片

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第2张图片

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第3张图片

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第4张图片

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第5张图片

2901e9463f24ee6b7078e2f112b06bad.png

声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息

/*

每一个学生都有对应归属地。

学生Student 地址 String。

学生属性:姓名,年龄。

注意:姓名和年龄相同的视为同一个学生。

保证学生的唯一性。

步骤:

1、描述学生。

2、定义map容器,将学生作为键,地址作为值,存入。

3、获取map集合中的元素。

*/

package a;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

public class lizi {

public static void main(String[] args) {

HashMap HSduixiang = new HashMap();

HSduixiang.put(new class_Student("张三", 22), "北京");

HSduixiang.put(new class_Student("李四", 25), "上海");

HSduixiang.put(new class_Student("王五", 24), "香港");

HSduixiang.put(new class_Student("小张", 24), "烟台");

HSduixiang.put(new class_Student("小李", 26), "济南");

System.out.println("第一种取出方式 ketSet:");

Set keySet = HSduixiang.keySet();

Iterator tiquchudeKey1 = keySet.iterator();

while (tiquchudeKey1.hasNext()) {

class_Student Student_name_age1 = tiquchudeKey1.next();

String addr = HSduixiang.get(Student_name_age1);

System.out.println(Student_name_age1 + "---" + addr);

}

System.out

.println("HSduixiang.get(Student_name_age)中的Student_name_age这里充当了Key,\n"

+ "就像引索,用关键词搜索出要相关的内容一样,这里的相关内容是地址addr");

System.out.println();

System.out.println("第二种取出方式entrySet:");

Set> entrySet = HSduixiang.entrySet();

Iterator> tiquchudeKey2 = entrySet.iterator();

while (tiquchudeKey2.hasNext()) {

Map.Entry yongMEdedaodeshujv = tiquchudeKey2.next();

class_Student Student_name_age2 = yongMEdedaodeshujv.getKey();

String addr = yongMEdedaodeshujv.getValue();

System.out.println(Student_name_age2 + "=====" + addr);

}

}

}

class class_Student implements Comparable {

private int age;

private String name;

class_Student(String name, int age) {

this.name = name;

this.age = age;

}

@Override

public int compareTo(class_Student s) {

// TODO 自动生成的方法存根:

int num = new Integer(this.age).compareTo(new Integer(s.age));

if (num == 0)

return this.name.compareTo(s.name);

return num;

}

@Override

public boolean equals(Object obj) {

if (!(obj instanceof class_Student))

throw new ClassCastException("类型不匹配");

class_Student s = (class_Student) obj;

return this.name.equals(s.name) && this.age == age;

}

public int getAge() {

return age;

}

public String getName() {

return name;

}

// 保证键的唯一性:

@Override

public int hashCode() {

return name.hashCode() + age * 34;

}

@Override

public String toString() {

return name + ":" + age;

}

}java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第6张图片

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第7张图片

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第8张图片

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第9张图片

bd4b10b2448e7e3bd976fa4c51f6cd27.png

java map存放班级和姓名_Java:声明学生类和HashMap,使用HashMap存储学生类,使用2种遍历方式遍历出学生信息..._第10张图片

你可能感兴趣的:(java,map存放班级和姓名)