以HashMap举例
- public class HashMap<K,V> extends AbstractMap<K,V>
- implements Map<K,V>, Cloneable, Serializable {
- //...
- transient volatile Set<K> keySet;
- public Set<K> keySet() {
- Set<K> ks;
- return (ks = keySet) == null ? (keySet = new KeySet()) : ks;
- }
- final class KeySet extends AbstractSet<K> {
- //...
- }
- //...
- }
复制代码
keySet()方法创建(创建后缓存在keySet变量中,方便下次快速访问)了一个KeySet类的对象,而KeySet是HashMap的一个内部类 |