将Hashmap与List list进行比较= new Arraylist()

时间:2012-03-06 04:20:20

标签: java arrays list hashmap

需要一些帮助:我不熟悉Hashmaps。如何将List和数组与哈希映射进行比较?清单&数组包含整数。

地图:

 Map<Integer, String> profMap = new HashMap<Integer, String>() {};
        while(profs.next()){
            Integer key= profs.getInt("profid");
            String name= profs.getString("profname");
            profMap.put(key,name);
        }
/* profs comes from my sql statement and pulls values from those columns*/

数组:

String [] profArray = request.getParameterValues("professor");

列表:

List list= new ArrayList();

我迷失在文档和我见过的所有其他示例中。希望有人可以帮助我在这里

2 个答案:

答案 0 :(得分:1)

根据您的评论,您希望比较键并存储与匹配的键相关联的值。

你可以这样做。

foreach(Integer key in profMap.keyset()) {
    if(/* Check the key against your criteria */) {
        list.add(ProfMap.get(key));
    }
}

答案 1 :(得分:0)

基本上,HashMap是由唯一键索引的项目列表。使用ArrayList或普通数组,您可以按列表中的位置进行索引,例如list [0]或list.get(0)。使用HashMap时,没有这样的排序,因此您无法通过索引引用项目。将项添加到HashMap时,您提供了一个键,可以是任何Object,以及值,可以是任何Object。如果要从地图中取回值,可以使用键值检索它。