简体   繁体   English

如何遍历哈希图中的哈希集?

[英]How can I iterate through a hashset within a hashmap?

I am trying to iterate through a hashmap which contains 8 entries.我正在尝试遍历包含 8 个条目的哈希图。 However one of these entries is a hashset with 2 objects within it.然而,这些条目之一是其中包含 2 个对象的哈希集。 II need to iterate through the hashset to get the values from these.我需要遍历哈希集以从中获取值。

First part of the code works, I loop through the hashmap and look for the key I require which is 'balloon'.代码的第一部分有效,我遍历哈希图并查找我需要的键“气球”。 I need help to iterate through the hashset to retrieve details from within the 2 balloon objects.我需要帮助来遍历哈希集以从 2 个气球对象中检索详细信息。

   List<PartyItem> myPartyList = new ArrayList<>();
    Map<String, Object> types = myPartyList .getPartyItems();
    for (Map.Entry<String, Object> entry : types.entrySet()) {
        if (StringUtils.contains(entry.getKey().toString(), "balloon")) {

           //This is where I need to loop through the balloon hashset to get the entries and values from within.
                myPartyList .add(balloon);

I then want to be able to take keys and values from myPartyList to assign them to variables in my code.然后我希望能够从 myPartyList 中获取键和值以将它们分配给我的代码中的变量。

Thanks谢谢

Test if a value is a Set and if it is, add all items to your list.测试一个值是否是一个Set ,如果是,则将所有项目添加到您的列表中。

if (StringUtils.contains(entry.getKey().toString(), "balloon")
    && entry.getValue() instanceof Set) {
   myPartyList.addAll((Set)entry.getValue());
}

Instead of iterating through entry just iterate through keys and when you find the balloon get the hashset to iterate through it.不是遍历条目,而是遍历键,当您找到气球时,请获取哈希集以遍历它。

for(String key: types.keySet()){
    if(StringUtils.contains(key, "balloon")){
        for(Object object: types.get(key)){
            //do what you need with object
        }
    }
}

You can iterate like this:你可以像这样迭代:

for(String key: Map.keySet()){
if(StringUtils.contains(key, "balloon")){
   Iterator<String> it = hashMap.get("balloon").iterator();
    while(it.hasNext()){
       // your code here
     }
   }
}

Usually you structure your hashmap as <key, value> and you access your values via their corresponding keys.通常,您将哈希映射构建为 <key, value> 并通过相应的键访问您的值。 But they have to match exactly.但它们必须完全匹配。 In your case your hashmap would look like this:在您的情况下,您的哈希图如下所示:

Map<String, Object> partyItems = myPartyList.getPartyItems();
// or maybe even
Map<String, PartyItem> partyItems = myPartyList.getPartyItems();

And getting the value is as easy as:获取价值就像这样简单:

Object partyItem = partyItems.get("baloon");

If you are not sure if your paryItems contain a value for your key baloon you can check that first:如果你不知道,如果你的paryItems包含你的关键字的值baloon ,你可以检查第一:

if (partyItems.contains("baloon")) {
    Object partyItem = partyItems.get("baloon");
}

If you are looking for a part of the key matching baloon :如果您正在寻找关键匹配baloon的一部分:

List<PartyItem> myFilteredPartyItems = partyItems.entrySet().stream()
    .filter(e -> e.getKey().contains("baloon"))
    .collect(Collectors.toList()))

This is called stream oriented programming (take a look at the Java Stream API ), and if your run at least Java 8 you can use those.这称为面向流的编程(查看Java Stream API ),如果您至少运行 Java 8,则可以使用它们。 And what it does, is turn the entries of the List to a stream, then remove everything which does not contain baloon in the key, and turn the resulting stream, which was not removed back to a list.它的作用是将 List 的条目转换为流,然后删除键中不包含baloon所有内容,并将未删除的结果流转换回列表。

Here you also find a very informative tutorial on how to use streams in Java.您还可以在此处找到有关如何在 Java 中使用流的信息丰富的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM