search
HomeJavajavaTutorialDetailed explanation of sample code for Map in Java collection

MapArchitecture:



##As shown above:

(1) Map is a mapping

interface , and the content stored in Map is a key-value pair (key-value)

(2) AbstractMap is

Inherited from Map's abstract class, which implements most of the API in Map.

(3) SortedMap is an interface inherited from Map. The content in SortedMap is sorted key-value pairs, and the sorting method is through a comparator.

(4) NavigableMap inherits from SortedMap, which has a series of navigation methods, such as "get key-value pairs greater than or equal to a certain

object" etc.

(5 ) TreeMap inherits from the AbstractMap and NavigableMap interfaces, so the contents in TreeMap are ordered key-value pairs.

(6) HashMap inherits from AbstractMap, and the content is also a key-value pair, but the order is not guaranteed.

(7) WeakHashMap inherits from AbstractMap, and its key type is different from HashMap. WeakHashMap is a weak key.

(8) HashTable inherits from

Directionary and also implements Map, so it is a key-value pair, but the order is not guaranteed, and it is thread-safe.

Summary:

HashMap is a hash table based on the "zipper method". It is generally used in single threads. The key values ​​​​can be empty and supports Iterator (iterator) traversal

Hashtable is a hash table implemented based on the "zipper method". It is thread-safe and can be used in multi-threaded programs. Supports two traversal methods: Iterator traversal and Enumeration.

WeakHashMap is also a hash table based on the "zipper method" and is also a weak key.

TreeMap is an ordered hash table, implemented through a red-black tree, and the key values ​​cannot be empty. .


Source code of Java8’s Map interface:

<p>public interface Map<k> {<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 v>> entrySet();//实体集合,Map的改变会影响到它<br>    interface Entry<k> {<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 href="http://www.php.cn/code/8202.html" target="_blank">super K>, V> Comparator<map.entry>> comparingByKey() {<br>            <a href="http://www.php.cn/wiki/135.html" target="_blank">return</a> (Comparator<map.entry v>> & Serializable)<br>                    (c1, c2) -> c1.getKey().compareTo(c2.getKey());<br>        }<br>        //比较两个值<br>        public static <k v extends comparable super>> Comparator<map.entry>> comparingByValue() {<br>            return (Comparator<map.entry v>> & Serializable)<br>                    (c1, c2) -> c1.getValue().compareTo(c2.getValue());<br>        }<br>        //比较两个key<br>        public static <k v> Comparator<map.entry 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 v>> & Serializable)<br>                    (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());<br>        }<br>        //比较两个值<br>        public static <k v> Comparator<map.entry v>> comparingByValue(Comparator super V> cmp) {<br>            Objects.requireNonNull(cmp);<br>            return (Comparator<map.entry 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></k></k></map.entry></map.entry></k></map.entry></map.entry></k></map.entry></map.entry></k></map.entry></map.entry></k></k></map.entry></v></k></k></p>

The above is the detailed content of Detailed explanation of sample code for Map in Java collection. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.