【发布时间】:2013-08-21 18:40:55
【问题描述】:
将HashMap<String,String> 添加到TreeSet<HashMap<String,String>> 时遇到以下问题。代码如下:
... Do something
XMLRoot root = XMLRoot
.newBuild()
.setTrx(
Tag
.newBuild()
.setName("trx")
.addAttribute("type", "04/01")
.addAttribute("id", id));
... => BreakPoint here!
现在addAttribute() 方法的作用是:
public Tag addAttribute(String name, String value) {
// Create the attribute.
HashMap<String, String> att = new HashMap<String, String>();
att.put(name, value);
attributes.add(att);
return this;
}
attributes 变量是 TreeSet<HashMap<String,String>> 类型的集合。现在使用 Netbeans 调试器,我在创建 XMLRoot 对象后立即添加了一个 BreakPoint,我发现它永远不会到达断点。问题是没有抛出异常,没有错误,什么都没有。另一个奇怪的事情是,如果我使用addAttribute() 方法只添加一个元素,那么一切正常。
问题:什么可能导致执行在 TreeSet 类的第二个元素的 add() 方法内终止...?
注意:使用调试器我设法看到第一个属性被设置,但我从未到达第二个属性,这意味着在添加第二个元素时执行突然结束。
详情: Apple JDK 1.6.0_51 64-Bit OSX 10.8.4 (Mountain Lion)
【问题讨论】:
-
当你说它永远不会到达断点时,它在哪里继续? JVM 会在那个时候终止吗?你确定你没有在某个地方吞下异常吗?哦,顺便说一句,如果值发生变化,使用可变对象作为集合中的值可能会导致问题。
-
JVM 没有结束,但是没有到达 BreakPoint。之后的代码也永远不会被执行。关于吞下一个例外......我不知道。线程也没有结束,因为我从 EDT 执行这条语句。