Heim  >  Artikel  >  Java  >  Ausführliche Erläuterung des Beispielcodes für die Map-in-Java-Sammlung

Ausführliche Erläuterung des Beispielcodes für die Map-in-Java-Sammlung

黄舟
黄舟Original
2017-03-13 17:42:081759Durchsuche

KarteArchitektur:



Wie oben gezeigt:

(1) Map ist eine Mapping--Schnittstelle , und der in Map gespeicherte Inhalt ist ein Schlüssel-Wert-Paar (Schlüssel-Wert)

( 2) AbstractMap ist Die abstrakte Klasse, die von Map erbt und den größten Teil der API in Map implementiert.

(3) SortedMap ist eine von Map geerbte Schnittstelle. Der Inhalt in SortedMap besteht aus sortierten Schlüssel-Wert-Paaren und die Sortiermethode erfolgt über einen Komparator.

(4) NavigableMap erbt von SortedMap, das über eine Reihe von Navigationsmethoden verfügt, z. B. „Erhalten Sie Schlüssel-Wert-Paare, die größer oder gleich einem bestimmten Objekt sind“ usw.

(5) TreeMap erbt von den Schnittstellen AbstractMap und NavigableMap, sodass die Inhalte in TreeMap geordnete Schlüssel-Wert-Paare sind.

(6) HashMap erbt von AbstractMap und der Inhalt ist ebenfalls ein Schlüssel-Wert-Paar, die Reihenfolge ist jedoch nicht garantiert.

(7) WeakHashMap erbt von AbstractMap und sein Schlüsseltyp unterscheidet sich von HashMap. WeakHashMap ist ein schwacher Schlüssel.

(8) HashTable erbt von Directionary und implementiert auch Map, es handelt sich also um ein Schlüssel-Wert-Paar, aber die Reihenfolge ist nicht garantiert und es ist threadsicher.

Zusammenfassung:

HashMap ist eine Hash-Tabelle, die auf der „Zipper-Methode“ basiert. Sie wird im Allgemeinen in einzelnen Threads verwendet und unterstützt Iterator (Iterator). ) Traversal

Hashtable ist eine Hash-Tabelle, die auf der „Zipper-Methode“ basiert. Sie ist threadsicher und kann in Multithread-Programmen verwendet werden. Unterstützt zwei Traversal-Methoden: Iterator-Traversal und Enumeration.

WeakHashMap ist auch eine Hash-Tabelle, die auf der „Zipper-Methode“ basiert und auch ein schwacher Schlüssel ist.

TreeMap ist eine geordnete Hash-Tabelle, die durch einen rot-schwarzen Baum und den Schlüsselwert implementiert wird darf nicht leer sein.


Quellcode der Kartenschnittstelle von Java8:

<p>public interface Map<K,V> {<br>    int size();//数目<br>    boolean isEmpty();//判断是否为空<br>    boolean containsKey(<a href="http://www.php.cn/wiki/60.html" target="_blank">Object</a> key);//判断是否含有某个key<br>    boolean containsValue(Object value);//判断是否含有某个值<br>    V get(Object key);//通过key获得value<br>    V put(K key, V value);//插入键值对<br>    V remove(Object key);//通过key<a href="http://www.php.cn/php/php-tp-remove.html" target="_blank">删除</a><br>    void put<a href="http://www.php.cn/wiki/1483.html" target="_blank">All</a>(Map<? <a href="http://www.php.cn/wiki/166.html" target="_blank">extends</a> K, ? extends V> m);//将一个Map插入<br>    void <a href="http://www.php.cn/wiki/917.html" target="_blank">clear</a>();//清空<br>    <a href="http://www.php.cn/code/8209.html" target="_blank">Set</a><K> keySet();//返回key集合<br>    Collection<V> values();//返回value<br>    Set<Map.Entry<K, V>> entrySet();//实体集合,Map的改变会影响到它<br>    interface Entry<K,V> {<br>        K getKey();//获得key<br>        V getValue();//获得value<br>        V setValue(V value);//设置值<br>        boolean equals(Object o);//判断对象是否相等<br>        int hashCode();//返回hashCode<br>        //比较器,比较两个key<br>        public <a href="http://www.php.cn/wiki/188.html" target="_blank">static</a> <K extends Comparable<? <a href="http://www.php.cn/code/8202.html" target="_blank">super</a> K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {<br>            <a href="http://www.php.cn/wiki/135.html" target="_blank">return</a> (Comparator<Map.Entry<K, V>> & Serializable)<br>                    (c1, c2) -> c1.getKey().compareTo(c2.getKey());<br>        }<br>        //比较两个值<br>        public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {<br>            return (Comparator<Map.Entry<K, V>> & Serializable)<br>                    (c1, c2) -> c1.getValue().compareTo(c2.getValue());<br>        }<br>        //比较两个key<br>        public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {<br>            Objects.<a href="http://www.php.cn/wiki/136.html" target="_blank">require</a>Non<a href="http://www.php.cn/wiki/62.html" target="_blank">Null</a>(cmp);<br>            return (Comparator<Map.Entry<K, V>> & Serializable)<br>                    (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());<br>        }<br>        //比较两个值<br>        public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {<br>            Objects.requireNonNull(cmp);<br>            return (Comparator<Map.Entry<K, V>> & Serializable)<br>                    (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());<br>        }<br>    }<br>    //比较map是否相等<br>    boolean equals(Object o);<br>    int hashCode();//hashCode<br>    default V get<a href="http://www.php.cn/wiki/1360.html" target="_blank">OrD</a>efault(Object key, V defaultValue) {<br>        V v;<br>        return (((v = get(key)) != null) || containsKey(key))<br>                ? v<br>                : defaultValue;<br>    }<br>    default void <a href="http://www.php.cn/wiki/127.html" target="_blank">forEach</a>(BiConsumer<? super K, ? super V> <a href="http://www.php.cn/java/java-Action.html" target="_blank">action</a>) {<br>        Objects.requireNonNull(action);<br>        for (Map.Entry<K, V> entry : entrySet()) {<br>            K k;<br>            V v;<br>            try {<br>                k = entry.getKey();<br>                v = entry.getValue();<br>            } catch(IllegalState<a href="http://www.php.cn/wiki/265.html" target="_blank">Exception</a> ise) {<br>                // this usually means the entry is no longer in the map.<br>                throw <a href="http://www.php.cn/wiki/165.html" target="_blank">new</a> Con<a href="http://www.php.cn/wiki/1046.html" target="_blank">current</a>Mod<a href="http://www.php.cn/wiki/109.html" target="_blank">if</a>icationException(ise);<br>            }<br>            action.accept(k, v);<br>        }<br>    }<br>    default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {<br>        Objects.requireNonNull(function);<br>        for (Map.Entry<K, V> entry : entrySet()) {<br>            K k;<br>            V v;<br>            try {<br>                k = entry.getKey();<br>                v = entry.getValue();<br>            } catch(IllegalStateException ise) {<br>                // this usually means the entry is no longer in the map.<br>                throw new ConcurrentModificationException(ise);<br>            }<br><br>            // ise thrown from function is not a cme.<br>            v = function.apply(k, v);<br><br>            try {<br>                entry.setValue(v);<br>            } catch(IllegalStateException ise) {<br>                // this usually means the entry is no longer in the map.<br>                throw new ConcurrentModificationException(ise);<br>            }<br>        }<br>    }<br>    default V putIfAbsent(K key, V value) {<br>        V v = get(key);<br>        if (v == null) {<br>            v = put(key, value);<br>        }<br><br>        return v;<br>    }<br>    //删除某个key和value对应的对象<br>    default boolean remove(Object key, Object value) {<br>        Object curValue = get(key);<br>        if (!Objects.equals(curValue, value) ||<br>                (curValue == null && !containsKey(key))) {<br>            return false;<br>        }<br>        remove(key);<br>        return true;<br>    }<br>    //将某个key和oldValue对应的值替换为newValue<br>    default boolean replace(K key, V oldValue, V newValue) {<br>        Object curValue = get(key);<br>        if (!Objects.equals(curValue, oldValue) ||<br>                (curValue == null && !containsKey(key))) {<br>            return false;<br>        }<br>        put(key, newValue);<br>        return true;<br>    }<br>    //替换key的值<br>    default V replace(K key, V value) {<br>        V curValue;<br>        if (((curValue = get(key)) != null) || containsKey(key)) {<br>            curValue = put(key, value);<br>        }<br>        return curValue;<br>    }<br>    default V computeIfAbsent(K key,<br>                              Function<? super K, ? extends V> mappingFunction) {<br>        Objects.requireNonNull(mappingFunction);<br>        V v;<br>        if ((v = get(key)) == null) {<br>            V newValue;<br>            if ((newValue = mappingFunction.apply(key)) != null) {<br>                put(key, newValue);<br>                return newValue;<br>            }<br>        }<br><br>        return v;<br>    }<br>    default V computeIfPresent(K key,<br>                               BiFunction<? super K, ? super V, ? extends V> remappingFunction) {<br>        Objects.requireNonNull(remappingFunction);<br>        V oldValue;<br>        if ((oldValue = get(key)) != null) {<br>            V newValue = remappingFunction.apply(key, oldValue);<br>            if (newValue != null) {<br>                put(key, newValue);<br>                return newValue;<br>            } <a href="http://www.php.cn/wiki/111.html" target="_blank">else</a> {<br>                remove(key);<br>                return null;<br>            }<br>        } else {<br>            return null;<br>        }<br>    }<br>    default V compute(K key,<br>                      BiFunction<? super K, ? super V, ? extends V> remappingFunction) {<br>        Objects.requireNonNull(remappingFunction);<br>        V oldValue = get(key);<br><br>        V newValue = remappingFunction.apply(key, oldValue);<br>        if (newValue == null) {<br>            // <a href="http://www.php.cn/wiki/1298.html" target="_blank">delete</a> mapping<br>            if (oldValue != null || containsKey(key)) {<br>                // something to remove<br>                remove(key);<br>                return null;<br>            } else {<br>                // nothing to do. Leave things as they were.<br>                return null;<br>            }<br>        } else {<br>            // add or replace old mapping<br>            put(key, newValue);<br>            return newValue;<br>        }<br>    }<br>    default V merge(K key, V value,<br>                    BiFunction<? super V, ? super V, ? extends V> remappingFunction) {<br>        Objects.requireNonNull(remappingFunction);<br>        Objects.requireNonNull(value);<br>        V oldValue = get(key);<br>        V newValue = (oldValue == null) ? value :<br>                remappingFunction.apply(oldValue, value);<br>        if(newValue == null) {<br>            remove(key);<br>        } else {<br>            put(key, newValue);<br>        }<br>        return newValue;<br>    }<br>}<br></p>

Das obige ist der detaillierte Inhalt vonAusführliche Erläuterung des Beispielcodes für die Map-in-Java-Sammlung. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn