简体   繁体   中英

How can I iterate through a hashset within a hashmap?

I am trying to iterate through a hashmap which contains 8 entries. However one of these entries is a hashset with 2 objects within it. 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.

   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.

Thanks

Test if a value is a Set and if it is, add all items to your list.

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. 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:

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

If you are looking for a part of the key matching 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. 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.

Here you also find a very informative tutorial on how to use streams in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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