java - entry
來源:趣味經驗館 2.47W
java中的entry是屬於一個靜態內部類,能夠實現Map.Entry<K,V>這個接口,而通過entry類可以構成一個單向鏈表。
java中的Map以及Map.Entry介紹
1、Map是屬於java中的接口,Map.Entry則是Map的一個內部接口。
2、Map提供了一些常用方法,比如keySet()、entrySet()等方法。
3、keySet()方法的返回值是Map中key值的集合;而entrySet()的返回值也是返回一個Set集合,此集合的類型是Map.Entry。
4、Map.Entry是Map聲明的一個內部接口,此接口屬於泛型,定義為Entry<K,V>。它表示Map中的一個實體。接口中會有getKey()、getValue方法。
參考範例:
entry類使用範例,示例代碼:
//源碼private static class Entry<K,V> implements Map.Entry<K,V> { int hash; final K key; V value; //next 可構成單向鏈表 Entry<K,V> next; protected Entry(int hash, K key, V value, Entry<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } protected Object clone() { return new Entry<>(hash, key, value,(next==null ? null : (Entry<K,V>) next.clone())); } // Map.Entry Ops public K getKey() { return key; } public V getValue() { return value; } public V setValue(V value) { if (value == null) throw new NullPointerException(); V oldValue = this.value; this.value = value; return oldValue; } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry<?,?> e = (Map.Entry)o; return key.equals(e.getKey()) && value.equals(e.getValue()); } public int hashCode() { return hash ^ value.hashCode(); } public String toString() { return key.toString()+"="+value.toString(); } }