How do I get a keyIterator for a LinkedHashMap?

前端 未结 3 1145
误落风尘
误落风尘 2021-02-13 09:43

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?

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-13 10:16

    You shouldn't use anything that is defined as part of the internal implementation of LinkedHashMap (i.e. in the source code but not defined in the API). What happens if the internal implementation changes in the next release? All of your code using it will break.

    You should code to the API and do something like

    myMap.keySet().iterator()
    

提交回复
热议问题