Chr*_*ris 1 java collections list hashmap map
我需要将结果集转换为地图地图.我没有使用地图列表的原因是因为我不想遍历整个List来获取特定的行.
我现在面临的问题是,如果索引大于16,则不再对HashMap的条目进行排序.
现在我尝试了以下一个简单的测试:
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
//creating 20 rows from 1 to 20
for (int i = 1; i <= 20; i++){
map.put(i, "ROW "+i);
}
//returning all the rows the map contains. Look at position 16.
for(int key : map.keySet()){
System.out.println(key);
}
}
Run Code Online (Sandbox Code Playgroud)
输出如下(查看位置16到20.它们完全无序):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 16 19 18 20
我真的很感激,如果有人能解释为什么会这样.
顺便说一下:
我不能使用这样的东西:
for (int i = 0; i < map.size; i++){
map.get(i) //...and so on
}
Run Code Online (Sandbox Code Playgroud)
因为我不知道索引是否存在.可能是200到800之间的索引不存在.因此,最好只迭代现有的地图条目.
A HashMap未订购或分类.如果您需要插入订购Map使用java.util.LinkedHashMap.如果您需要Map按键排序,请使用SortedMap如java.util.TreeMap
| 归档时间: |
|
| 查看次数: |
6505 次 |
| 最近记录: |