s-day04--Map集合的介绍和基本使用
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.inmind.map01;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/*
|
||||
Interface Map<K,V>
|
||||
参数类型
|
||||
K - 键的类型
|
||||
V - 值的类型
|
||||
常用实现类:
|
||||
HashMap LinkedHashMap TreeMap
|
||||
|
||||
常用的数据操作方法:
|
||||
- public V put(K key, V value): 把指定的键与指定的值添加到Map集合中。
|
||||
- public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
|
||||
- public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
|
||||
- public boolean containKey(Object key):判断该集合中是否有此键。
|
||||
|
||||
|
||||
- public Set<K> keySet(): 获取Map集合中所有的键,存储到Set集合中。
|
||||
- public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。
|
||||
|
||||
*/
|
||||
public class MapDemo01 {
|
||||
public static void main(String[] args) {
|
||||
//Map的多态
|
||||
//请创建出一个key为整数,value为字符串的双列集合
|
||||
Map<Integer,String> map = new HashMap<>();
|
||||
//- public V put(K key, V value): 把指定的键与指定的值添加到Map集合中。
|
||||
map.put(1, "刘备");//新增
|
||||
map.put(2, "吕布");
|
||||
map.put(3, "张飞");
|
||||
System.out.println(map);
|
||||
//修改吕布这个值
|
||||
String upValue = map.put(2, "关羽");//已有键的put就是修改,并返回被修改掉的值
|
||||
System.out.println(upValue);
|
||||
map.put(4, "貂蝉");
|
||||
System.out.println(map);
|
||||
|
||||
//- public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
|
||||
System.out.println(map.remove(4));
|
||||
System.out.println(map);
|
||||
|
||||
//- public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
|
||||
System.out.println(map.get(2));
|
||||
|
||||
//- public boolean containKey(Object key):判断该集合中是否有此键。
|
||||
System.out.println(map.containsKey(2));//true
|
||||
System.out.println(map.containsKey(5));//false
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user