当前位置:懂科普 >

IT科技

> java entry

java entry

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方法。

java entry

参考范例:

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();        }    }
标签: entry java
  • 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/itkeji/rw4qod.html