各位好!
为什么我执行完Map测试后是有序的呢?代码如下:
public class MapTest {
public HashMapstudents;
public MapTest(){
this.students=new HashMap();
}
public void testput(){
Scanner input=new Scanner(System.in);
int i=0;
while (i<3){
System.out.print("请输入学生ID:");
Integer id=input.nextInt();
student st=students.get(id);
if (st == null){
System.out.print("请输入 学生姓名:");
String name=input.next();
student newstudent=new student(id,name);
students.put(id, newstudent);
System.out.println("添加学生成功"+students.get(id).name);
i++;
}else{
System.out.print("该学生id被占用 ");
continue;
}
}
}
public void testkeyset(){
SetkeSet=students.keySet();
System.out.println("总共有:"+students.size()+"个学生");
for (Integer stt:keSet){
student st=students.get(stt);
if(st !=null){
System.out.println("学生:"+st.name);
}
}
}
这个是执行完的结果!
请输入学生ID:1
请输入 学生姓名:a
添加学生成功a
请输入学生ID:2
请输入 学生姓名:b
添加学生成功b
请输入学生ID:3
请输入 学生姓名:c
添加学生成功c
总共有:3个学生
学生:a
学生:b
学生:c