首页 > Java > java教程 > 正文

Java集合之Map的示例代码详解

黄舟
发布: 2017-03-13 17:42:08
原创
2112人浏览过

Map架构



如上图:

(1)Map是映射接口,Map中存储的内容是键值对(key-value)

(2)AbstractMap是继承于Map的抽象类,实现了Map中的大部分API

(3)SortedMap是继承于Map的接口,SortedMap中的内容是排序的键值对,排序的方法是通过比较器。

(4)NavigableMap继承于SortedMap,其中有一系列的导航方法,如“获取大于或等于某对象的键值对”等等

(5)TreeMap继承于AbstractMap和NavigableMap接口,因此TreeMap中的内容是有序的键值对。

(6)HashMap继承于AbstractMap,内容也是键值对,但不保证次序。

(7)WeakHashMap继承于AbstractMap,它和HashMap的键类型不同,WeakHashMap是弱键。

(8)HashTable继承于Directionary同时也实现了Map,因此是键值对的,但不保证次序,同时是线程安全的。

总结:

HashMap是基于”拉链法“实现的散列表,一般用于单线程,键值都可以为空,支持Iterator(迭代器)遍历

Hashtable是基于”拉链法“实现的散列表,是线程安全的,可以用于多线程程序中。支持Iterator(迭代器)遍历和Enumeration(枚举器)两种遍历方式。

WeakHashMap也是基于”拉链法“实现的散列表,同时是弱键

TreeMap 是有序的散列表,通过红黑树来实现的,键值都不能为空。


Java8的Map接口的源码:

<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>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/735">
                            <img src="https://img.php.cn/upload/ai_manual/001/246/273/68b6cdbf48df2598.png" alt="代码小浣熊">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/735">代码小浣熊</a>
                            <p>代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="代码小浣熊">
                                <span>51</span>
                            </div>
                        </div>
                        <a href="/ai/735" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="代码小浣熊">
                        </a>
                    </div>
                
登录后复制

以上就是Java集合之Map的示例代码详解的详细内容,更多请关注php中文网其它相关文章!

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号