我们都知道Set(除了它们的这种实现)doesn't guarantee
的迭代排序。所以我试图确保这与下面的示例代码。Java集订购
public static void main(String[] args) throws InterruptedException {
Map<String,String> lMap=new HashMap<String, String>();
lMap.put("A", "A");
lMap.put("B", "B");
lMap.put("C", "C");
lMap.put("D", "D");
lMap.put("E", "E");
lMap.put("F", "F");
lMap.put("G", "G");
lMap.put("H", "H");
lMap.put("I", "I");
lMap.put("J", "J");
lMap.put("K", "K");
lMap.put("L", "L");
for(int i=0;i<10000;i++){
Thread.sleep(100);
Set<Entry<String, String>> entrYset=lMap.entrySet();
for(Map.Entry<String, String> e:entrYset){
System.out.println(e.getKey()+" , "+e.getValue());
}
System.out.println("******************************************************");
}
}
我在代码上执行了很多次,发现它是打印记录的顺序。
我的问题是,如果java声称HashMap是无序的,那么为什么这个记录打印的顺序。如果有人可以给我理由,例如,这将是伟大的。
你为什么要印10000次?如果地图未被修改,订单将始终保持不变。 – gontard