Replace HashMap keys with values in Property file in Java












4















I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?



KeyMapping.properties

newKey1 oldLKey1
newKey2 oldKey2


//Load property mapping file
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");

Enumeration<String> newKeys = properties.getKeys();
Map<String, Object> result = new LinkedHashMap<>();

while (newKeys.hasMoreElements()) {
String newKey = (String) newKeys.nextElement();
Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();

while(iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();

//If key matches the key in property file
if (entry.getKey().equals(newKey)) {

//remove the entry from map mapToReplaceKeys
iterator.remove();

//add the key with the 'oldKey' and existing value
result.put(properties.getString(newKey), entry.getValue());
}
}
}









share|improve this question





























    4















    I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?



    KeyMapping.properties

    newKey1 oldLKey1
    newKey2 oldKey2


    //Load property mapping file
    ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");

    Enumeration<String> newKeys = properties.getKeys();
    Map<String, Object> result = new LinkedHashMap<>();

    while (newKeys.hasMoreElements()) {
    String newKey = (String) newKeys.nextElement();
    Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();

    while(iterator.hasNext()) {
    Entry<String, Object> entry = iterator.next();

    //If key matches the key in property file
    if (entry.getKey().equals(newKey)) {

    //remove the entry from map mapToReplaceKeys
    iterator.remove();

    //add the key with the 'oldKey' and existing value
    result.put(properties.getString(newKey), entry.getValue());
    }
    }
    }









    share|improve this question



























      4












      4








      4


      1






      I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?



      KeyMapping.properties

      newKey1 oldLKey1
      newKey2 oldKey2


      //Load property mapping file
      ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");

      Enumeration<String> newKeys = properties.getKeys();
      Map<String, Object> result = new LinkedHashMap<>();

      while (newKeys.hasMoreElements()) {
      String newKey = (String) newKeys.nextElement();
      Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();

      while(iterator.hasNext()) {
      Entry<String, Object> entry = iterator.next();

      //If key matches the key in property file
      if (entry.getKey().equals(newKey)) {

      //remove the entry from map mapToReplaceKeys
      iterator.remove();

      //add the key with the 'oldKey' and existing value
      result.put(properties.getString(newKey), entry.getValue());
      }
      }
      }









      share|improve this question
















      I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?



      KeyMapping.properties

      newKey1 oldLKey1
      newKey2 oldKey2


      //Load property mapping file
      ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");

      Enumeration<String> newKeys = properties.getKeys();
      Map<String, Object> result = new LinkedHashMap<>();

      while (newKeys.hasMoreElements()) {
      String newKey = (String) newKeys.nextElement();
      Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();

      while(iterator.hasNext()) {
      Entry<String, Object> entry = iterator.next();

      //If key matches the key in property file
      if (entry.getKey().equals(newKey)) {

      //remove the entry from map mapToReplaceKeys
      iterator.remove();

      //add the key with the 'oldKey' and existing value
      result.put(properties.getString(newKey), entry.getValue());
      }
      }
      }






      java java-8 hashmap






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 4:07









      nullpointer

      44.6k1096184




      44.6k1096184










      asked Nov 21 '18 at 23:38









      user679526user679526

      45731133




      45731133
























          2 Answers
          2






          active

          oldest

          votes


















          4














          What you're essentially doing is this:



          Map<String, Object> result = Collections.list(properties.getKeys())
          .stream()
          .flatMap(element -> mapToReplaceKeys.entrySet()
          .stream()
          .filter(entry -> entry.getKey().equals(element)))
          .collect(toMap(e -> properties.getString(e.getKey()),
          Map.Entry::getValue,
          (l, r) -> r,
          LinkedHashMap::new));


          or you could also do:



          Map<String, Object> result = new LinkedHashMap<>();
          newKeys.asIterator()
          .forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
          if(k.equals(e)) result.put(properties.getString(k), v);
          }));





          share|improve this answer


























          • Iterating over a Map, to find an equal key … that feels so … wrong…

            – Holger
            Nov 22 '18 at 13:11











          • @Holger now that you mention it, I feel so silly...

            – Aomine
            Nov 22 '18 at 13:32



















          1














          Don’t iterate over a Map, just to check the keys for equality. That’s what the Map’s dedicated lookup methods are for:



          ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
          Map<String, Object> result = new LinkedHashMap<>();

          for(String newKey: properties.keySet()) {
          Object value = mapToReplaceKeys.remove(newKey);
          if(value != null) result.put(properties.getString(newKey), value);
          }


          Since you want to remove the mappings, you can just use remove on the Map, which will do nothing and just return null when the key is not present.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421961%2freplace-hashmap-keys-with-values-in-property-file-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            What you're essentially doing is this:



            Map<String, Object> result = Collections.list(properties.getKeys())
            .stream()
            .flatMap(element -> mapToReplaceKeys.entrySet()
            .stream()
            .filter(entry -> entry.getKey().equals(element)))
            .collect(toMap(e -> properties.getString(e.getKey()),
            Map.Entry::getValue,
            (l, r) -> r,
            LinkedHashMap::new));


            or you could also do:



            Map<String, Object> result = new LinkedHashMap<>();
            newKeys.asIterator()
            .forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
            if(k.equals(e)) result.put(properties.getString(k), v);
            }));





            share|improve this answer


























            • Iterating over a Map, to find an equal key … that feels so … wrong…

              – Holger
              Nov 22 '18 at 13:11











            • @Holger now that you mention it, I feel so silly...

              – Aomine
              Nov 22 '18 at 13:32
















            4














            What you're essentially doing is this:



            Map<String, Object> result = Collections.list(properties.getKeys())
            .stream()
            .flatMap(element -> mapToReplaceKeys.entrySet()
            .stream()
            .filter(entry -> entry.getKey().equals(element)))
            .collect(toMap(e -> properties.getString(e.getKey()),
            Map.Entry::getValue,
            (l, r) -> r,
            LinkedHashMap::new));


            or you could also do:



            Map<String, Object> result = new LinkedHashMap<>();
            newKeys.asIterator()
            .forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
            if(k.equals(e)) result.put(properties.getString(k), v);
            }));





            share|improve this answer


























            • Iterating over a Map, to find an equal key … that feels so … wrong…

              – Holger
              Nov 22 '18 at 13:11











            • @Holger now that you mention it, I feel so silly...

              – Aomine
              Nov 22 '18 at 13:32














            4












            4








            4







            What you're essentially doing is this:



            Map<String, Object> result = Collections.list(properties.getKeys())
            .stream()
            .flatMap(element -> mapToReplaceKeys.entrySet()
            .stream()
            .filter(entry -> entry.getKey().equals(element)))
            .collect(toMap(e -> properties.getString(e.getKey()),
            Map.Entry::getValue,
            (l, r) -> r,
            LinkedHashMap::new));


            or you could also do:



            Map<String, Object> result = new LinkedHashMap<>();
            newKeys.asIterator()
            .forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
            if(k.equals(e)) result.put(properties.getString(k), v);
            }));





            share|improve this answer















            What you're essentially doing is this:



            Map<String, Object> result = Collections.list(properties.getKeys())
            .stream()
            .flatMap(element -> mapToReplaceKeys.entrySet()
            .stream()
            .filter(entry -> entry.getKey().equals(element)))
            .collect(toMap(e -> properties.getString(e.getKey()),
            Map.Entry::getValue,
            (l, r) -> r,
            LinkedHashMap::new));


            or you could also do:



            Map<String, Object> result = new LinkedHashMap<>();
            newKeys.asIterator()
            .forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
            if(k.equals(e)) result.put(properties.getString(k), v);
            }));






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 0:47

























            answered Nov 22 '18 at 0:36









            AomineAomine

            41.8k74071




            41.8k74071













            • Iterating over a Map, to find an equal key … that feels so … wrong…

              – Holger
              Nov 22 '18 at 13:11











            • @Holger now that you mention it, I feel so silly...

              – Aomine
              Nov 22 '18 at 13:32



















            • Iterating over a Map, to find an equal key … that feels so … wrong…

              – Holger
              Nov 22 '18 at 13:11











            • @Holger now that you mention it, I feel so silly...

              – Aomine
              Nov 22 '18 at 13:32

















            Iterating over a Map, to find an equal key … that feels so … wrong…

            – Holger
            Nov 22 '18 at 13:11





            Iterating over a Map, to find an equal key … that feels so … wrong…

            – Holger
            Nov 22 '18 at 13:11













            @Holger now that you mention it, I feel so silly...

            – Aomine
            Nov 22 '18 at 13:32





            @Holger now that you mention it, I feel so silly...

            – Aomine
            Nov 22 '18 at 13:32













            1














            Don’t iterate over a Map, just to check the keys for equality. That’s what the Map’s dedicated lookup methods are for:



            ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
            Map<String, Object> result = new LinkedHashMap<>();

            for(String newKey: properties.keySet()) {
            Object value = mapToReplaceKeys.remove(newKey);
            if(value != null) result.put(properties.getString(newKey), value);
            }


            Since you want to remove the mappings, you can just use remove on the Map, which will do nothing and just return null when the key is not present.






            share|improve this answer




























              1














              Don’t iterate over a Map, just to check the keys for equality. That’s what the Map’s dedicated lookup methods are for:



              ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
              Map<String, Object> result = new LinkedHashMap<>();

              for(String newKey: properties.keySet()) {
              Object value = mapToReplaceKeys.remove(newKey);
              if(value != null) result.put(properties.getString(newKey), value);
              }


              Since you want to remove the mappings, you can just use remove on the Map, which will do nothing and just return null when the key is not present.






              share|improve this answer


























                1












                1








                1







                Don’t iterate over a Map, just to check the keys for equality. That’s what the Map’s dedicated lookup methods are for:



                ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
                Map<String, Object> result = new LinkedHashMap<>();

                for(String newKey: properties.keySet()) {
                Object value = mapToReplaceKeys.remove(newKey);
                if(value != null) result.put(properties.getString(newKey), value);
                }


                Since you want to remove the mappings, you can just use remove on the Map, which will do nothing and just return null when the key is not present.






                share|improve this answer













                Don’t iterate over a Map, just to check the keys for equality. That’s what the Map’s dedicated lookup methods are for:



                ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
                Map<String, Object> result = new LinkedHashMap<>();

                for(String newKey: properties.keySet()) {
                Object value = mapToReplaceKeys.remove(newKey);
                if(value != null) result.put(properties.getString(newKey), value);
                }


                Since you want to remove the mappings, you can just use remove on the Map, which will do nothing and just return null when the key is not present.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 13:10









                HolgerHolger

                163k23231438




                163k23231438






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421961%2freplace-hashmap-keys-with-values-in-property-file-in-java%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python