Loop through n number of maps

iterate through a map c++
iterate map in java
iterate map of map in java 8
how to iterate map inside map in java
iterate over map javascript
best way to iterate hashmap in java
java map
how to iterate two maps simultaneously in java

Right now I have the following code, which takes 2 recipes and finds duplicates in the recipes and "merges" them.

public void mergeIngredients(Recipe recipe1, Recipe recipe2) {

    Map<String, Ingredients> recipe1Map = recipe1.getIngredientsMap();
    Map<String, Ingredients> recipe2Map = recipe2.getIngredientsMap();

    for (Map.Entry<String, Ingredients> s : recipe1Map.entrySet()) {

        if (recipe2Map.containsKey(s.getKey())) {
            double newValue = recipe1.getAmount(s.getKey()) + recipe2.getAmount(s.getKey());

I want to change this code so instead of only being able to check 2 maps against each other, I need to refactor the code so it can take N number of maps and compare all of them.

Example: The user inputs 8 different recipes, it should loop through all of these and merge ingredients if duplicates are found. What is the best way to achieve this?

I would first extract all keys from all Maps into a Set. This gives you all unique ingredients-keys.

Then iterate that Set and get all the values from all the recipes and merge them.

For example:

public void mergeIngredients(Set<Recipe> recipes) {

    Set<String> keys = recipes.stream()         //
            .map(Recipe::getIngredientsMap)     // Get the map
            .flatMap(m -> m.keySet().stream())  // Get all keys and make 1 big stream 
            .collect(Collectors.toSet());       // Collect them to a set

    for (String k : keys)
        double newValue = recipes.stream()       //
                .map(Recipe::getIngredientsMap)  //
                .map(i->i.get(k))                //
                .mapToDouble(i->i.getAmount())   //
                .sum();                          //


You problably can do this more efficient; but this is easier to follow I think.

Loop through n number of maps, Loop through n number of maps. iterate through a map c++ iterate map in java how to iterate map inside map in java iterate map of map in java 8 iterate over  @Parham const before or after a type specified is a matter of preference, but I choose to keep it on the right because it makes it clearer in situations where pointers are being used; for instance when using both int const *x and int *const x you can write it as int const *const x which is much clearer IMO than const int *const x.

You can use Merging Multiple Maps Using Java 8 Streams in the case of duplicate keys:

public void mergerMap() throws Exception {
    Map<String, Integer> m1 = ImmutableMap.of("a", 2, "b", 3);
    Map<String, Integer> m2 = ImmutableMap.of("a", 3, "c", 4);

    Map<String, Integer> mx = Stream.of(m1, m2)
        .map(Map::entrySet)          // converts each map into an entry set
        .flatMap(Collection::stream) // converts each set into an entry stream, then
                                     // "concatenates" it in place of the original set
            Collectors.toMap(        // collects into a map
                Map.Entry::getKey,   // where each entry is based
                Map.Entry::getValue, // on the entries in the stream
                Integer::max         // such that if a value already exist for
                                     // a given key, the max of the old
                                     // and new value is taken

    Map<String, Integer> expected = ImmutableMap.of("a", 3, "b", 3, "c", 4);
    assertEquals(expected, mx);

6 ways to iterate or loop a Map in Java, Using foreach in Java 8. If you using Java 8 this is the easiest way to loop the Map. 1. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 27. // {. Map<Integer  Varun September 17, 2016 How to Iterate over a map in C++ 2016-09-17T20:47:03+05:30 std::map, STL 1 Comment In this article we will discuss 3 different ways to Iterate over a map in C++. Suppose we have a map of string and int as key-value pair i.e.

I don't really see the need of a Map for your ingredients so here is an alternative solution. If you make your Ingredients class implement equals & hashcode you can use it directly in a Set. You will of course also have a method in Recipe that returns all ingredients as a List. Then the following will return all unique ingredients.

Set<Ingredients> merge(List<Recipe> recipies) {
    return recipies.stream().map(s -> s.allIngredients()).collect(Collectors.toSet()); 

3 Examples to Loop Map in Java, There are multiple ways to loop through Map in Java, you can either use a foreach loop or Iterator to Set<Map.Entry<Integer, Worker>> entrySet = workersById.entrySet(); for (Map. getValue(); System.out.printf("%s :\t %d %n", worker. - How to loop a Map in Java. It is cheap, but not free. I gives you extra complexity to your code. You will feel it every time, when you will have to process 100 messages per second.

Best Way to Iterate Over Each Entry of HashMap in Java, This is also fastest way to iterate over Map in Java. Entry<Integer, String> entry : map.entrySet()){ System.out.printf("Key : %s and Value: %s %n", entry. K-map is basically a diagram made up of squares. Each of these squares represents a min-term of the variables. If n = number of variables then the number of squares in its K-map will be 2 n. K-map is made using the truth table. In fact, it is a special form of the truth table that is folded upon itself like a sphere.

Combinatorial Maps: Efficient Data Structures for Computer , Algorithm 31: Iterator over one dart per each i-cell for n-maps Input: cm: an n-map​; The complexity of Algorithm 31 is linear in the number of darts of the n- map A second example given in Algorithm 32 consists in iterating through one dart  Iterating through a golang map. Ask Question Asked 8 years, 7 months ago. Active 1 year ago. How do I loop through or enumerate a JavaScript object? 118.

How to iterate any Map in Java, Problem in comparing Floating point numbers and how to compare them correctly? Find local businesses, view maps and get driving directions in Google Maps.

  • Possible duplicate of Merging two Map<String, Integer> with Java 8 Stream API
  • Thanks! I'm getting a NullPointer on this line though double newValue = recipes.stream().map(Recipe::getIngredientsMap).map(i->i.get(k)).mapToDouble(Ingredients::getAmount).sum(); Do you know what could be the issue?
  • How does this answer the question? (multiple maps)
  • It's because I need 3 elements for each ingredient: amount + unit + description Example: 1 pound chicken
  • @FrederikJorgensen, not sure I understand. Wouldn't that mean that your Ingredients class has 3 members; amount, ´unit` and description? Why is a map necessary because of this?