本文介绍了看不到HashMap的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输出到达flag2,但我看不到HashMap内部的内容。代码如下: public class traffic_analysis {
static HashMap< InetAddress,Integer> ; mp = new HashMap< InetAddress,Integer>();
static void SrcCnt(InetAddress src_ip){
InetAddress SourceIP = src_ip;
System.out.println(SourceIP);
if(mp.get(SourceIP)== null){
mp.put(SourceIP,0);
System.out.println(---- MPIKE ----);
} else {
mp.put(SourceIP,mp.get(SourceIP)+1);
System.out.println(---- MPIKE XANA ----);
}
System.out.println(mp.toString());
static void PrintMap(){
System.out.println(---- EIMAI EDW ----);
Iterator iterator = mp.keySet()。iterator();
while(iterator.hasNext()){
System.out.println(---- flag ----);
String key = iterator.next()。toString();
System.out.println(---- flag2 ----);
String value = mp.get(key).toString();
System.out.println(---- ---- flag3 ----);
System.out.println(key +blabla+ value);
}
}
}
在实际的关键。当您尝试使用字符串获取它时,将会失败。试试这个:
InetAddress key = iterator.next();
System.out.println(---- flag2 ----);
String value = mp.get(key).toString();
System.out.println(---- ---- flag3 ----);
Output reaches flag2 but I can't see what is inside the HashMap. The code is:
public class traffic_analysis {
static HashMap<InetAddress,Integer> mp=new HashMap<InetAddress, Integer>();
static void SrcCnt(InetAddress src_ip) {
InetAddress SourceIP = src_ip;
System.out.println(SourceIP);
if (mp.get(SourceIP) == null){
mp.put(SourceIP, 0);
System.out.println("----MPIKE----");
}else {
mp.put(SourceIP,mp.get(SourceIP)+1);
System.out.println("----MPIKE XANA----");
}
System.out.println(mp.toString());
}
static void PrintMap() {
System.out.println("----EIMAI EDW----");
Iterator iterator = mp.keySet().iterator();
while (iterator.hasNext()) {
System.out.println("----flag----");
String key = iterator.next().toString();
System.out.println("----flag2----");
String value = mp.get(key).toString();
System.out.println("----flag3----");
System.out.println(key + "blabla " + value);
}
}
}
Where is the problem?
解决方案
You're calling toString
on the actual key. When you try to get it with the string, that will fail. Try this instead:
InetAddress key = iterator.next();
System.out.println("----flag2----");
String value = mp.get(key).toString();
System.out.println("----flag3----");
这篇关于看不到HashMap的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!