Map.Entry源代码问题
涯若 2017-02-22 08:37:14 Map集合在遍历的时候可以使用迭代器iterator,也可以使用Map.Entry : map.entrySet()来foreach。查看其源代码,即entrySet()方法的源代码,发现不理解的问题。先贴出源代码:
public Set<Map.Entry<K,V>> entrySet() {
return entrySet0();
}
private Set<Map.Entry<K,V>> entrySet0() {
Set<Map.Entry<K,V>> es = entrySet;
return es != null ? es : (entrySet = new EntrySet());
}
EntrySet类的代码省略。不理解之处在于为什么不直接在entrySet()方法中实现entrySet0中的代码,而是通过调用entrySet0方法。是因为public和private吗?如果是,是为了什么?本人java能力尚浅,还望赐教。谢谢