By looking at the source code for LinkedHashMaps from Sun, I see that there is a private class called KeyIterator, I would like to use this. How can I gain access?
Share
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.
You get it by calling
You shouldn’t even need to know it exists; it’s just an artifact of the implementation. For all you know, they could be using flying monkeys to iterate the keys; as long as they’re iterated according to the spec, it doesn’t matter how they do it.
By the way, did you know that
HashMaphas a private class calledKeyIterator(as doConcurrentHashMap,ConcurrentSkipListMap,EnumMap,IdentityHashMap,TreeMap, andWeakHashMap)?Does that make a difference in how you iterate through the keys of a
HashMap?Edit: In reponse to your comment, be aware that if you are trying to iterate over all key-value pairs in a
Map, there is a better way than iterating over the keys and callinggetfor each. TheentrySet()method gets aSetof all key-value pairs which you can then iterate over. So instead of writing:you should write:
You could also iterate over the values with
values()if you want.Note that since
keySet,entrySet, andvaluesare defined in theMapinterface, they will work for anyMap, not justLinkedHashMap.