这篇文章给大家聊聊关于map.get,以及map.get(key)返回值对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。
在Java编程语言中,HashMap是一个非常常用的数据结构,而map.get方法则是HashMap中一个非常重要的操作。它就像一把钥匙,能够帮助我们轻松地找到我们想要的数据。map.get方法究竟有什么魅力?它在编程中又有哪些应用呢?今天,我们就来聊聊这个话题。
一、map.get方法简介
我们来简单了解一下map.get方法。map.get方法属于HashMap类,它接受一个键(key)作为参数,返回与该键关联的值(value)。如果键不存在,则返回null。
代码示例:
“`java
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap
map.put(“
Map 只会 put、get快来学这几个“新”方法
Map是编程中常用的数据接口,JDK8引入了多个新方法以简化对Map中数据的操作。以下是这些方法的详细介绍和示例:
getOrDefault
功能:尝试获取key对应的值,如果未获取到,则返回默认值。
示例:
private static void testGetOrDefault(){Map<String, String> map= new HashMap<>(4);map.put("123","123");String key="key";String defaultValue="defaultValue";//老写法String oldValue= defaultValue;if(map.containsKey(key)){oldValue= map.get(key);}System.out.println("oldValue="+ oldValue);//新写法String newValue= map.getOrDefault(key, defaultValue);System.out.println("newValue="+ newValue);}forEach
功能:遍历Map的数据,可以更方便地使用entry中的key和value。
示例:
private static void testForeach(){Map<String, String> map= new HashMap<>(4);map.put("123","123");//老写法for(Map.Entry<String, String> entry: map.entrySet()){System.out.printf("老写法 key=%s, value=%s%n", entry.getKey(), entry.getValue());}//新写法map.forEach((key, value)-> System.out.printf("新写法 key=%s, value=%s%n", key, value));}merge
功能:合并entry。如果key存在,将value按照function计算后更新到Map中;如果key不存在,将key-value放入Map中。
示例:
private static void testMerge(){Map<String, Integer> cntMap= new HashMap<>(8);List<String> list= Arrays.asList("apple","orange","banana","orange");//老写法for(String item: list){if(cntMap.containsKey(item)){cntMap.put(item, cntMap.get(item)+ 1);} else{cntMap.put(item, 1);}}//新写法for(String item: list){cntMap.merge(item, 1, Integer::sum);}}putIfAbsent
功能:不存在key或者值为null时,才将键值对放入Map。与put方法相比,不会直接覆盖已有的值。
示例:
private static void testPutIfAbsent(){Map<String, Integer> scoreMap= new HashMap<>(4);scoreMap.put("Jim", 88);scoreMap.put("Lily", 90);//老写法if(!scoreMap.containsKey("Lily")){scoreMap.put("Lily", 98);}//新写法scoreMap.putIfAbsent("Lily", 98);}compute
功能:传入key和function,获取key对应的oldValue(可能为null),经过function计算获取newValue,然后执行put(key, newValue)。
示例:
private static void testComputer(){Map<String, Integer> cntMap= new HashMap<>(8);List<String> list= Arrays.asList("apple","orange","banana","orange");//老写法for(String item: list){if(cntMap.containsKey(item)){cntMap.put(item, cntMap.get(item)+ 1);} else{cntMap.put(item, 1);}}//新写法for(String item: list){cntMap.compute(item,(k, v)->{if(v== null){v= 1;} else{v+= 1;}return v;});}}computeIfAbsent
功能:只在key不存在时执行compute计算。如果key对应的value存在,则直接返回该value。
示例:
private static void testComputerIfAbsent(){Map<Integer, Integer> fabMap= new ConcurrentHashMap<>(16);fabMap.put(0, 1);fabMap.put(1, 1);System.out.println(fab(5, fabMap));}private static Integer fab(Integer index, Map<Integer, Integer> fabMap){return fabMap.computeIfAbsent(index, i-> fab(i- 2, fabMap)+ fab(i- 1, fabMap));}computeIfPresent
功能:只有key存在时才执行compute计算和值的更新。
replace
功能:如果key存在,则更新值;如果key不存在,则不执行任何操作。
这些方法在特定场景下可以显著简化代码,提高代码的可读性和优雅性。
java map .put 方法
Map.Put方法原理:
Map将对象table赋值给tab,并以tab是否为空作为是否第一次调用此方法的判断,是则resize()并给tab,n赋值;
获取tab的第i个元素:根据(n- 1)& hash算法,计算出i找到,如果为空,调用newNode(),赋值给tab第i个;
如果不为空,可能存在2种情况:hash值重复了,也就是put过程中,发现之前已经有了此key对应的value,则暂时e= p;
至于另外一种情况就是位置冲突了,即根据(n- 1)& hash算法发生了碰撞,再次分情况讨论;
1、以链表的形式存入;
2、如果碰撞导致链表过长(大于等于TREEIFY_THRESHOLD),就把链表转换成红黑树;
3、最后,如果e不为空,将e添加到table中(e.value被赋值为 putVal()中的参数 value);
扩展资料:
HashMap存储键值对时,发现找到的位置上已经存储了元素。
情况一:调用该元素键的equals方法与该位置上元素的键进行比较,如果返回ture,则视新键与已经存在的键相同,用新值去更新旧值,然后put方法返回旧值。
情况二:调用该元素键的equals方法与该位置上元素的键进行比较,如果返回false,则新键与已经存在的键不相同,任然可以将新的元素存储在该位置。
HashMap的put()方法返回null的特殊情况:
要是已经存在键的映射,但是值是null,那么调用put()方法再更新键的值时,put()方法会把旧值null返回,这是返回null的特殊情况。存在元素时,要是找到的位置上没有键的映射,put()方法也是返回null。
参考资料:Map(Map接口)_百度百科
java中如何使用map存取数据
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Map
*– HashMap
*-特点: 1、可以使用NULL值和NULL键
*2、不同步
*(除了非同步和允许使用NULL,其他与HashTable没什么区别)
*-方法
*如下
*@author caihai
*
*/
public class HashMapDemo{
public static void main(String args[])
{
System.out.println(“HashMap:———————————-“);
Map<String,Integer> hashmap=new HashMap<String,Integer>();
//按键-值的方式存入数据
hashmap.put(“1”, 1);
hashmap.put(“2”,2);
hashmap.put(“4”,4);
hashmap.put(“3”,3);
hashmap.put(null,null);
//containsKey
System.out.println(“判断是否含有”1“此键”+hashmap.containsKey(“1”));
System.out.println(“——————————————-“);
//containsValue
System.out.println(“判断时候含有”1“此值”+hashmap.containsValue(1));
System.out.println(“——————————————-“);
//遍历MAP的二种方法
//keySet
System.out.println(“利用keyset方式遍历MAP”);
Set<String> keyset=hashmap.keySet();
for(String ks:keyset)
{
System.out.println(“keyset—key:”+ks+” value:”+hashmap.get(ks));
}
System.out.println(“——————————————-“);
System.out.println(“利用entrySet方式遍历MAP”);
Set<Map.Entry<String, Integer>> entryset=hashmap.entrySet();
for(Map.Entry<String,Integer> entry:entryset)
{
System.out.println(“entryset—key:”+entry.getKey()+” value:”+entry.getValue());
}
System.out.println(“——————————————-“);
System.out.println(“判断Hashmap是否为空”+hashmap.isEmpty());
System.out.println(“——————————————-“);
System.out.println(“通过get(Object key)获得对应值”+hashmap.get(null));
System.out.println(“——————————————-“);
System.out.println(“计算Map的大小”+hashmap.size());
Map<String,Integer> insertmap=new HashMap<String,Integer>();
insertmap.put(“100”,100);
insertmap.put(“101”,101);
insertmap.put(“102”,102);
System.out.println(“——————————————-“);
System.out.println(“将MAP加入到MAP中”);
hashmap.putAll(insertmap);
Set<String> keyseti=hashmap.keySet();
for(String ks:keyseti)
{
System.out.println(“key:”+ks+” value:”+hashmap.get(ks));
}
System.out.println(“——————————————-“);
System.out.println(“Get the Map values,return Collection:”);
Collection<Integer> values=hashmap.values();
Iterator<Integer> it=values.iterator();
while(it.hasNext())
{
System.out.println(“The value:”+it.next());
}
}
}
关于map.get的内容到此结束,希望对大家有所帮助。




