我有一个将值放入HashMap<String, Object[]>
类型的HashMap中并返回相同HashMap的方法。
将值放入HashMap的代码:
doc = Jsoup.connect(url).get();
for( org.jsoup.nodes.Element element : doc.getAllElements() )
{
for( Attribute attribute : element.attributes() )
{
String option_ID=element.tagName()+"_"+attribute.getKey()+"_"+attribute.getValue();
String HTMLText=element.text();
int HTMLTextSize=HTMLText.length();
if(!HTMLText.isEmpty())
data.put("Test"+i,new Object[{"Test"+i,option_ID,HTMLText,HTMLTextSize});//adding value in HashMap.
i++;
}
}
我尝试如下迭代,我认为这不是正确的方法:
HashMap<String, Object[]>set=HTMLDocument.createHTMLSet("URL of website");
Iterator it = set.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
当我得到的输出为:
Test79 = [Ljava.lang.Object; @ 14e1a0f
Test378 = [Ljava.lang.Object; @ 1a5f880
我应该如何遍历此HashMap以获得Object []值,例如option_ID,HTMLText?
最佳答案
由于每个对象都有toString()
方法,因此默认显示类名表示形式,然后添加@
符号和哈希码,这就是为什么要获取输出的原因
[Ljava.lang.Object;@14e1a0f
这意味着该数组包含一个类或接口。
一种解决方案是在数组上循环并打印每个部分(或使用
Arrays.toString
方法),但是我强烈建议您将其包装到自己的类中并覆盖toString
方法。