2025年Java map接口方法测试

Java map接口方法测试Map 是一种把键对象和值对象进行映射的集合 Map 对象每次存储两个值 键值 key 和数值 value 它们合起来称为键值对 每个 key 最多对应一个 value 其中 key 不可重复 一个 Map 对象的所有 key 值相当于一个 set 集合 不同的 key 可以对应相同的 value map 接口方法测试 增

大家好,我是讯享网,很高兴认识大家。

Map是一种把键对象和值对象进行映射的集合,Map对象每次存储两个值,键值key和数值value,它们合起来称为键值对。

每个key最多对应一个value,其中key不可重复,一个Map对象的所有key值相当于一个set集合。

不同的key可以对应相同的value。

map接口方法测试

增: 

V put(K key, V value):插入键值对 void putAll(Map<? extends K,? extends V> m):复制指定的图的所有键值对到该对象 default V putIfAbsent(K key, V value):如果传入的key映射的value为空,则将传入的 value映射到此key,返回null;若不为空,则返回当前值。 

讯享网
讯享网Map<Integer,String> map=new HashMap() ; map.put(1,"one") ;

 

 Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; TreeMap<Integer,String> tree=new TreeMap<>() ; tree.put(1,"二傻子") ; tree.put(2,"two") ; tree.put(3,"three"); map.putAll(tree); Set<Integer> key=map.keySet() ; for(Integer i:key){ System.out.println(map.get(i)); } D:\Java\jdk\bin\java.exe 二傻子 two three Process finished with exit code 0 

从上面可以看出,当key(1)存在映射value(“one”)时,旧的value(“one”)会被新存入的value(“二傻子”)覆盖

讯享网Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; System.out.println(map.putIfAbsent(2,"five")); System.out.println(map.putIfAbsent(1,"我爱你")+"\n"); Set<Integer> key=map.keySet() ; for(Integer i:key){ System.out.println(map.get(i)); } D:\Java\jdk\bin\java.exe null one one five Process finished with exit code 0

map集合的遍历

map集合主要由三种遍历方式

方法一:将map集合中的所有key值转换为一个set集合,通过set的迭代器或者forEach循环调用map的get方法实现遍历:

 Set<Integer> key=map.keySet() ;         for(Integer i:key){             System.out.println(map.get(i));         }

 方法二:调用map的entrySet方法,将map中的所有键值对都转换为一个Set集合,其中Set集合的数据类型是map的一个内部类Entry<k,v>.如下。

讯享网 Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); }

方法三:通过forEach()遍历。 

Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); map.forEach((Key,value)->{ System.out.println("["+Key+","+value+"]"); }); D:\Java\jdk\bin\java.exe [1,one] [2,two] [3,three]

 感觉forEach最容易。

以上就是遍历方法啦,没有提到的欢迎提出哦。

 删:

讯享网void clear():清空集合中的所有键值对,不多说 V remove(Object key):如果存在key,则删除key和value,并返回value default boolean remove(Object key, Object value):只有当key和value对应时才删除他们。 

 

 Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); map.put(4,"four") ; map.put(5,"five") ; System.out.println(map.remove(2) ); System.out.println(map.remove(7)); Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); } Set<Integer> S=map.keySet() ; for(Integer i:S){ System.out.print(i); } D:\Java\jdk\bin\java.exe two null 1=one 3=three 4=four 5=five 1345 Process finished with exit code 0

 以上代码,发现map的remove方法在删除value的同时会删除key值

讯享网 Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); System.out.println(map.remove(2,"222") ); Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); } System.out.println("---------------"); System.out.println(map.remove(2,"two")); Set<Integer> S=map.keySet() ; for(Integer i:S){ System.out.println(map.get(i)); } false 1=one 2=two 3=three --------------- true one three

改: 

default V replace(K key, V value):只有当key和value对应时才能替换它们

default boolean replace(K key, V oldValue, V newValue):只有当key和oldValue对应时才能用newValue替换oldValue.


讯享网

default void replaceAll(BiFunction<? super K,? super V,? extends V> function):

将所有键值对的value按照指定函数变换为新的值.

 Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); map.replace(1,"111") ; Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); }

 感觉这个replace方法相当于如果存在key,就改变他的值,看来API文档后相当于

讯享网if (map.containsKey(key)){ return map.put(key, value); } else return null; 

 第二个replace比较直观

Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); boolean b1= map.replace(1,"one","111") ; boolean b2=map.replace(2,"twoo","222") ; System.out.println(b1+" "+b2); Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); } D:\Java\jdk\bin\java.exe true false 1=111 2=two 3=three Process finished with exit code 0

replaceAll()

讯享网Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); //将所有的value都转换为大写 map.replaceAll((Key,value)->value.toUpperCase()); Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); } D:\Java\jdk\bin\java.exe 1=ONE 2=TWO 3=THREE Process finished with exit code 0

 (key, value) -> do 是匿名函数 lambda 表达式,do主要写改变的函数,当然只能改变value,就记住这样搞.

接下来是最牛的forEach()

 map.forEach((Key,value)->{ //key变为原来的平方,value变为大写,并打印key和value。 Key=Key*Key; value=value.toUpperCase() ; System.out.println("["+Key+","+value+"]"); }); D:\Java\jdk\bin\java.exe [1,ONE] [4,TWO] [9,THREE] Process finished with exit code 0

 不知道的点就是forEach该咋写,格式forEach((key,value)->{  });

查: 

讯享网V get(Object key) 如果key有value,返回value,无则返回null。 boolean isEmpty() 判空,没有键值对返回true。 boolean containsKey(Object key):如果此key存在,则返回 true 。 boolean containsValue(Object value):如果存在指定的value,则返回 true 。 int size() 返回此集合中键值对的数量。 boolean equals(Object o):比较相等于,不多说 int hashCode() 返回此地图的哈希码值。 重点测试下面几个: Set<Map.Entry<K,V>> entrySet():返回此地图中包含的映射的Set视图。 Set<K> keySet(): 返回此地图中包含的键的Set视图。 Collection<V> values():将该map对象的所有value存储在一个Collextion对象中,并返回。 default V getOrDefault(Object key, V defaultValue):如果存在key,则返回相应的value,不存在则返回defaultValue默认值。 

entrySet()和keySet()主要用于遍历,前面已经提到

 Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); } Set<Integer> S=map.keySet() ; for(Integer i:S){ System.out.println(map.get(i)); } D:\Java\jdk\bin\java.exe 1=one 2=two 3=three one two three Process finished with exit code 0

 Collection<V> values()

讯享网Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); Collection<String> values=map.values() ; Iterator it=values.iterator() ; while (it.hasNext()){ System.out.println(it.next()); } D:\Java\jdk\bin\java.exe one two three Process finished with exit code 0 

方法getOrDefault(Object key, V defaultValue) 

 Map<Integer,String> map=new HashMap() ; map.put(1,"one") ; map.put(2,"two") ; map.put(3,"three"); System.out.println(map.getOrDefault(1,"not found that key")); System.out.println(map.getOrDefault(4,"not found that key")); Set<Map.Entry<Integer,String>> set=map.entrySet(); for(Map.Entry<Integer,String> i:set){ System.out.println(i); } D:\Java\jdk\bin\java.exe one not found that key 1=one 2=two 3=three

发现如果没有指定的key,getOrDefault并不会将默认值写入,只是返回一下。

以上内容有错误的地方欢迎提出。

 

小讯
上一篇 2025-02-22 11:14
下一篇 2025-04-03 11:19

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/124603.html