广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java map中相同的key保存多个value值方式
  • 573
分享到

java map中相同的key保存多个value值方式

2024-04-02 19:04:59 573人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录map中相同的key保存多个value值如下代码Map中相同的键Key不同的值Value实现原理实现原理总结map中相同的key保存多个value值 在java中,Map集合中只

map中相同的key保存多个value值

在java中,Map集合中只能保存一个相同的key,如果再添加相同的key,则之后添加的key的值会覆盖之前key对应的值,Map中一个key只存在唯一的值。

如下代码


package test; 
import org.junit.Test; 
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map; 
import static java.util.Objects.hash; 
public class HashMapTest {
 
    @Test
    public void test0() {
        String str1 = new String("key");
        String str2 = new String("key");
        System.out.println(str1 == str2);
 
        Map<String,String> map = new HashMap<String,String>();
        map.put(str1,"value1");
        map.put(str2,"value2");//会覆盖之前的值,map长度为1
        
 
        for(Map.Entry<String,String> entry:map.entrySet()){
            System.out.println(entry.geTKEy()+"  "+entry.getValue());
        }
        System.out.println("------->"+map.get("key"));
    }
控制台输出如下: 
 
    
    @Test
    public void test1(){
        String str1 = "key";
        String str2 = "key";
        System.out.println(str1 == str2);
        Map<String,String> map = new IdentityHashMap<>();
        map.put(str1,"value1");
        map.put(str2,"value2");
        for(Map.Entry<String,String> entry:map.entrySet()){
            System.out.println(entry.getKey()+"  "+entry.getValue());
        }
        System.out.println("containsKey---->"+map.get("key"));
        System.out.println("value---->"+map.get("key"));
    }
 控制台输出如下 
 
 
 
    @Test
    public void test2(){
        String str1 = new String("key");
        String str2 = new String("key");
        System.out.println(str1 == str2);
        Map<String, String> map = new IdentityHashMap<>();
        map.put(str1,"value1");
        map.put(str2,"value2");
        for(Map.Entry<String,String> entry:map.entrySet()){
            System.out.println(entry.getKey()+"  "+entry.getValue());
        }
        System.out.println("\"key\" containKey--->"+map.containsKey("key"));
        System.out.println("str1 containKey--->"+map.containsKey(str1));
        System.out.println("str2 containKey--->"+map.containsKey(str2));
        System.out.println("value--->"+map.get("key"));
        System.out.println("value--->"+map.get(str1));
        System.out.println("value--->"+map.get(str2));
    }
 控制台输出如下:
 
  
 
    
    private class CustomObject{
        private String value; 
        public CustomObject(String value){
            this.value = value;
        }
 
        public String getValue() {
            return value;
        }
 
        public void setValue(String value) {
            this.value = value;
        }
 
         
 
        @Override
        public int hashCode() {
            if(value !=null){
                return super.hashCode()+hash(value);
            }else{
                return super.hashCode();
            }
        }
 
        @Override
        public boolean equals(Object obj) {
            if(this == obj){
                return true;
            }
            if(obj == null || getClass() != obj.getClass()){
                return false;
            }
            CustomObject object = (CustomObject) obj;
            if(this.value != null && this.value.equals(object.getValue())){
                return true;
            }
            if(this.value == null && object.value == null){
                return true;
            }
            return false;
        }
    } 
}

Map中相同的键Key不同的值Value实现原理

Map中相同的键Key对应不同的值Value通常出现在树形结构的数据处理中,通常的实现方法有jdk提供的IdentityHashMap和spring提供的MultiValueMap。


public static void main(String[] args) {
	Map<String, Object> identity = new IdentityHashMap<>();
	identity.put("A", "A");
	identity.put("A", "B");
	identity.put("A", "C");
	Map<String, Object> identityString = new IdentityHashMap<>();
	identityString.put(String.join("A", ""), "B");
	identityString.put("A", "A");
	identityString.put(new String("A"), "C");
	MultiValueMap<String, Object> linked = new LinkedMultiValueMap<>();
	linked.add("A", "A");
	linked.add("A", "B");
	linked.add("A", "C");
	for (String key : identity.keySet()) {
		System.out.println("identity:" + identity.get(key));
	}
	for (String key : identityString.keySet()) {
		System.out.println("identity string:" + identityString.get(key));
	}
	for (String key : linked.keySet()) {
		System.out.println("linked:" + linked.get(key));
	}
}

实现原理

  • JDK提供的IdentityHashMap其底层是根据Key的hash码的不同+transient Object[] table来实现的;
  • Spring提供的LinkedMultiValueMap其底层是使用LinkedHashMap来实现的;
  • LinkedHashMap的底层是使用transient Entry<K, V> head和transient Entry<K, V> tail来实现的;
  • Entry是LinkedHashMap的内部类,其定义方式为:

static class Entry<K, V> extends HashMap.node<K, V> { Entry<K, V> before; Entry<K, V> after; }

总结

IdentityHashMap和LinkedMultiValueMap的实现归根结底就是数组链表的使用。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: java map中相同的key保存多个value值方式

本文链接: https://www.lsjlt.com/news/133871.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • java map中相同的key保存多个value值方式
    目录map中相同的key保存多个value值如下代码Map中相同的键Key不同的值Value实现原理实现原理总结map中相同的key保存多个value值 在java中,Map集合中只...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作