从 google HashMultimap 访问元素时出现问题
我正在使用下面的代码来获取 &来自google HashMultimap的过程值
HashMultimap hmm = new HashMultimap();
HashMultimap hmm2 = new HashMultimap();
Element ele;
:
hmm2.put("name","Amit");
hmm.put("Amit",ele);
hmm.put("rohit",hmm2);
:
Iterator itr = hmm.keys().iterator();
String ky = (String) itr.next();
System.out.println(hmm.get(ky));
ky = (String) itr.next();
System.out.println(hmm.get(ky));
在上面的代码中,如果映射元素(或条目)是元素类型,那么我想做一些操作。如果是HashMultimap类型则进行其他操作。我如何检查对象并将其传递给另一个函数。
I am using below code to get & process value from google HashMultimap
HashMultimap hmm = new HashMultimap();
HashMultimap hmm2 = new HashMultimap();
Element ele;
:
hmm2.put("name","Amit");
hmm.put("Amit",ele);
hmm.put("rohit",hmm2);
:
Iterator itr = hmm.keys().iterator();
String ky = (String) itr.next();
System.out.println(hmm.get(ky));
ky = (String) itr.next();
System.out.println(hmm.get(ky));
In above code, if map element(or entry) is Element type then i want to do some operation. If it is HashMultimap type then do some other operation. How can i check and pass the object to another function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于这是一个
HashMultimap
,它也是一个SetMultimap
,因此当您调用hmm.get(ky)
时,返回的值将是 < code>Set 该键的所有值。然后,您应该能够迭代Set
中的每个值并对这些值使用instanceof
。如果每个键不会有多个值,则您不应首先使用 Multimap,而应仅使用普通的 HashMap。关键点是,在
HashMultimap
上调用get()
会返回一个Set
值,而不是单个值。Since this is a
HashMultimap
which is also aSetMultimap
, when you callhmm.get(ky)
the value returned is going to be aSet
of all the values for that key. You should then be able to iterate through each of the values in theSet
and useinstanceof
on those values. If there are not going to be multiple values for each key, you shouldn't be using aMultimap
to begin with and should just use normalHashMap
.The key point is that calling
get()
on aHashMultimap
returns aSet
of values and not a single value.使用instanceof关键字。
就像这样:
if (item instanceof Element) //做事情
Use the instanceof keyword.
Like so:
if (item instanceof Element) //Do things