我的set为什么是顺序输出
跟老师一样敲的。但是我是顺序输出。我用的是MyEclipse。不是set是随机吗。我试了至少4次不一样的东西了都是顺序
package com.imooc.collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
public Map<String,Student>students;
public MapTest() {
this.students=new HashMap<String,Student>();
}
public void testPut() {
Scanner console=new Scanner(System.in);
int i=0;
while(i<3) {
System.out.println("请输出学生ID:");
String ID=console.next();
Student st=students.get(ID);
if(st==null) {
System.out.println("请输入学生姓名:");
String name=console.next();
Student newStudent=new Student(ID,name);
students.put(ID, newStudent);
System.out.println("成功添加学生:"+students.get(ID).name);
i++;
}else {
System.out.println("该学生ID已被占用");
}
}
}
public void testKeySet() {
Set<String>keySet=students.keySet();
for(String stuId:keySet) {
Student st=students.get(stuId);
if(st!=null)
System.out.println("学生:"+st.name);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MapTest mt=new MapTest();
mt.testPut();
mt.testKeySet();
}
}