迭代HashMap [英] Iterating through a HashMap

查看:159
本文介绍了迭代HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我目前正在研究一种搜索方法,搜索的术语会遍历数据库,并将匹配的产品添加到带有2个Integer字段的哈希映射中。



<然后我无法获得散列表打印出详细信息。



这是我的代码

p>

  public HashMap< Integer,Integer> bankSearch = new HashMap< Integer,Integer>(); 

和用法

  Iterator it = bankSearch.entrySet()。iterator(); 
while(it.hasNext()){
HashMap.Entry pairs =(HashMap.Entry)it.next();
System.out.println(pairs.getKey()+=+ pairs.getValue());
if(bankItemsN [i]> 254){
outStream.writeByte(255);
outStream.writeDWord_v2(pairs.getValue());
} else {
outStream.writeByte(pairs.getValue()); //数量
}
if(bankItemsN [i] <1){
bankItems [i] = 0;
}
outStream.writeWordBigEndianA(pairs.getKey()); // itemID
}

当前错误

  .\src\client.java:75:找不到符号
符号:类迭代器
位置:类客户端
迭代器它= bankSearch.entrySet()。iterator();
^
.\src\client.java:77:java.util.HashMap.Entry在java.util.HashMap中是非公开的
;不能从外部包访问
HashMap.Entry pairs =(HashMap.Entry)it.next();
^
.\src\client.java:77:java.util.HashMap.Entry在java.util.HashMap中是非公开的
;不能从外部包访问
HashMap.Entry pairs =(HashMap.Entry)it.next();
^
3错误
按任意键继续。 。 。


解决方案

你得到的错误是由于: p>


  • 您没有导入 java.util.Iterator


  • HashMap.Entry 是一个私有的内部类。您应该使用 Map.Entry




正如templatetypedef所说的,使用Iterator的通用版本,或者使用for-each构造。


$ b

附加



以下是一些实际代码,展示了两种方法:

  import java.util.Map; 
import java.util.HashMap;
import java.util.Iterator;

public class MapExample {
public static void main(String [] args){
Map< String,Integer> m = new HashMap< String,Integer>();
m.put(One,1);
m.put(Two,2);
m.put(Three,3);

//使用for-each
(Map.Entry< String,Integer> e:m.entrySet()){
System.out.println(e。 getKey()+=>+ e.getValue());
}

//使用迭代器
Iterator< Map.Entry< String,Integer>> it = m.entrySet()。iterator();
while(it.hasNext()){
Map.Entry e =(Map.Entry< String,Integer>)it.next();
System.out.println(e.getKey()+=>+ e.getValue());
}
}
}


Okay so i'm currently working on a searching method, the terms searched are ran through the database and the matching products are added to a hashMap with 2 Integer fields.

then after the hashmap is made, the items are to be shown, however i'm having trouble getting the hashmap to print out the details

here's my code

public HashMap<Integer, Integer> bankSearch = new HashMap<Integer, Integer>();

and the use

    Iterator it = bankSearch.entrySet().iterator();
    while (it.hasNext()) {
        HashMap.Entry pairs = (HashMap.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
        if (bankItemsN[i] > 254) {
            outStream.writeByte(255);
            outStream.writeDWord_v2(pairs.getValue());
        } else {
            outStream.writeByte(pairs.getValue()); // amount
        }
        if (bankItemsN[i] < 1) {
            bankItems[i] = 0;
        }
        outStream.writeWordBigEndianA(pairs.getKey()); // itemID
    }

current errors

.\src\client.java:75: cannot find symbol
symbol  : class Iterator
location: class client
                Iterator it = bankSearch.entrySet().iterator();
                ^
.\src\client.java:77: java.util.HashMap.Entry is not public in java.util.HashMap
; cannot be accessed from outside package
                        HashMap.Entry pairs = (HashMap.Entry)it.next();
                               ^
.\src\client.java:77: java.util.HashMap.Entry is not public in java.util.HashMap
; cannot be accessed from outside package
                        HashMap.Entry pairs = (HashMap.Entry)it.next();
                                                      ^
3 errors
Press any key to continue . . .

解决方案

The errors you are getting are due to:

  • You did not import java.util.Iterator

  • HashMap.Entry is a private inner class. You should use Map.Entry

Also you should, as templatetypedef says, use the generic version of Iterator, or use a for-each construct.

ADDENDUM

Here is some actual code, demonstrating both approaches:

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

public class MapExample {
    public static void main(String[] args) {
        Map<String, Integer> m = new HashMap<String, Integer>();
        m.put("One", 1);
        m.put("Two", 2);
        m.put("Three", 3);

        // Using a for-each
        for (Map.Entry<String, Integer> e: m.entrySet()) {
            System.out.println(e.getKey() + " => " + e.getValue());
        }

        // Using an iterator
        Iterator<Map.Entry<String, Integer>> it = m.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry<String, Integer>)it.next();
            System.out.println(e.getKey() + " => " + e.getValue());
        }
    }
}

这篇关于迭代HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆