I’m interested how I can very quickly change the Boolean values into this hashmap:
HashMap<String, Boolean> selectedIds = new HashMap<>();
I want very quickly to replace the Boolean values all to be true. How I can do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The fastest way is this:
This code avoids any lookups whatsoever, because it iterates though the entire map’s entries and sets their values directly.
Note that whenever
HashMap.put()is called, a key look up occurs in the internalHashtable. While the code is highly optimized, it nevertheless requires work to calculate and compare hashcodes, then employ an algorithm to ultimately find the entry (if it exists). This is all "work", and consumes CPU cycles.Java 8 update:
Java 8 introduced a new method
replaceAll()for just such a purpose, making the code required even simpler: