我有两个HashMap对象定义如下:
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
我还有第三个HashMap对象:
HashMap<String, Integer> map3;
Run Code Online (Sandbox Code Playgroud)
我该如何合并map1,并map2汇集成map3?
如果我使用:
HashMap<String, Integer> test = new HashMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
或者我使用:
HashMap test = new HashMap();
Run Code Online (Sandbox Code Playgroud)
我可以在测试对象上应用的其他方法有什么不同.像test.put(),test.get()等,如果初始化不同??
此外,如果我在测试对象中放置一些东西,例如:
test.put("One", new Integer(5));
test.put("Two", new Integer(4));
test.put("Three", new Integer(3));
Run Code Online (Sandbox Code Playgroud)
并将其显示为:
设置set = tokens.entrySet();
Iterator ik = test.iterator();
while(ik.hasNext()){
Map.Entry me = (Map.Entry)ik.next();
System.out.println(me.getKey() + " : " + me.getValue() );
Run Code Online (Sandbox Code Playgroud)
结果没有排序,restul是:
三:3一:5两:1
它遵循什么规则?输出的这种正常行为是否随机显示?
您好我想通过应用标准从HashMap中删除项目.考虑以下代码:
Set<Foo> set = myMap.keySet();
Iterator<Foo> itr = set.iterator();
while (itr.hasNext())
{
Foo foo = itr.next();
if (foo.toString().length() < 3) {
myMap.remove(foo); //remove the pair if key length is less than 3
}
}
Run Code Online (Sandbox Code Playgroud)
所以我得到运行时ConcurentModification Exception,因为在迭代期间我正在修改HashMap.我该怎么办?有没有其他方法来搜索我的crieteria并在最后执行remove命令,以便我可以避免这个异常?
考虑以下代码:
if (int a == 0) {
System.out.println("hello");
continue;
}
Run Code Online (Sandbox Code Playgroud)
这if是forjava中循环的一部分.这里继续声明的意义何在?我知道continue它是相反的,break因此它不会突破循环,而只是跳过迭代它下面的任何东西.但是如果它在if声明中,我真的需要这样吗?