Map介绍以及常用的四种方法

Map

Map介绍:

Map<k,v>集合

  • Map集合的特点
  • 1.Map集合是一个双列集合,一个元素包含两个值(一个key,一个value)
  • 2.Map集合中的元素,key和value的数据类型可以是相同的也可以是不同的
  • 3.Map集合中的元素,key是不允许重复的,value是可以重复的
  • 4.Map集合中的元素,key和value是一一对应的
  • java.util.HashMap<K,V>集合implements Map<K,V>接口
  • 特点:
  • 底层是哈希表,查询的速度特别快
  • 1jdk1.8之前:数组+单链表
  • jdk1.8之后:数组+单向链表/红黑树(链表的长度超过8):提高查询速度
  • 2基于哈希表的实现的Map接口。 此实现提供了所有可选的地图操作,并允许null的值和null键。 ( HashMap类大致相当于Hashtable ,除了它是不同步的,并允许null)。这个类不能保证地图的顺序; 特别是,它不能保证订单在一段时间内保持不变。 */
    //public class LinkedHashMap<K,V>
    //extends HashMap<K,V>
    //implements Map<K,V>
    //特点:集合底层是哈希表+链表(保证迭代的顺序)
    //2.集合是一个有序的集合,存储元素和取出元素的顺序是一致的

1.Map的put方法

代码:

	/*put(K key, V value)返回v值
将指定的值与该映射中的指定键相关联(可选操作)。*/
private static void demo01() {
    Map<String ,String> map=new HashMap<>();
    String v1 = map.put("李晨", "范冰冰1");
    System.out.println("v1:"+v1);//v1:null,返回的map中key对应的v值
    String v2 = map.put("李晨", "范冰冰2");
    System.out.println("v2:"+v2);//v2:范冰冰1
    System.out.println(map);
    map.put("冷锋", "龙小云");
    map.put("李晨", "李小璐");
    map.put("李晨", "范冰冰");
    map.put("中国:", "龙小云");
    System.out.println(map);//{李晨=范冰冰, 中国:=龙小云, 冷锋=龙小云}key不重复,value可以重复!
	}

注意

返回值虽然是value值,但是是Map中关键字已经包含的value值,key值是不允许重复的,所以,put同样的key值,value值是更新的,但是返回值却是原来key对应的值。

2.Map的get方法

代码:

/*V get(Object key)
返回到指定键所映射的值,或 null如果此映射包含该键的映射。 */
private static void demo03() {
    Map<String ,Integer> map=new HashMap<>();
    map.put("迪丽热巴1",171 );
    map.put("迪丽热巴2",172 );
    map.put("迪丽热巴3",173 );
    map.put("迪丽热巴4",174 );
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}
    Integer v1 = map.get("迪丽热巴4");
    System.out.println("V1:"+v1);//V1:174
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}
    Integer v2 = map.get("迪丽热巴9");
    System.out.println("V2:"+v2);//V2:null
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}
}

2.Map的remove方法

代码:

 /*remove(Object key) 返回v值
如果存在(从可选的操作),从该地图中删除一个键的映射。*/
private static void demo02() {
    Map<String ,Integer> map=new HashMap<>();
    map.put("迪丽热巴1",171 );
    map.put("迪丽热巴2",172 );
    map.put("迪丽热巴3",173 );
    map.put("迪丽热巴4",174 );
    System.out.println(map);
    Integer v = map.remove("迪丽热巴2");
    System.out.println("v:"+v);//v:172
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴3=173}
    Integer v1 = map.remove("迪丽热巴9");
    System.out.println("v1:"+v1);//v1:null
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴3=173}
}

4.Map的containskey方法

代码:

  /*containsKey(Object key)
如果此映射包含指定键的映射,则返回 true 。*/
private static void demo04() {
    Map<String ,Integer> map=new HashMap<>();
    map.put("迪丽热巴1",171 );
    map.put("迪丽热巴2",172 );
    map.put("迪丽热巴3",173 );
    map.put("迪丽热巴4",174 );
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}
    boolean b = map.containsKey("迪丽热巴2");
    System.out.println("b:"+b);//true
    boolean b1 = map.containsKey("迪丽热巴9");
    System.out.println("b1:"+b1);//false

}

主方法分别调用各个方法:

  public static void main(String[] args) {
 demo01();
 demo02();
   demo03();
    demo04();
}

完整代码:

public class Demo01Mao {
public static void main(String[] args) {
    //demo01();
    //demo02();
   // demo03();
    demo04();
}
/*containsKey(Object key)
如果此映射包含指定键的映射,则返回 true 。*/
private static void demo04() {
    Map<String ,Integer> map=new HashMap<>();
    map.put("迪丽热巴1",171 );
    map.put("迪丽热巴2",172 );
    map.put("迪丽热巴3",173 );
    map.put("迪丽热巴4",174 );
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}
    boolean b = map.containsKey("迪丽热巴2");
    System.out.println("b:"+b);//true
    boolean b1 = map.containsKey("迪丽热巴9");
    System.out.println("b1:"+b1);//false

}

/*V get(Object key)
返回到指定键所映射的值,或 null如果此映射包含该键的映射。 */
private static void demo03() {
    Map<String ,Integer> map=new HashMap<>();
    map.put("迪丽热巴1",171 );
    map.put("迪丽热巴2",172 );
    map.put("迪丽热巴3",173 );
    map.put("迪丽热巴4",174 );
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}
    Integer v1 = map.get("迪丽热巴4");
    System.out.println("V1:"+v1);//V1:174
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}
    Integer v2 = map.get("迪丽热巴9");
    System.out.println("V2:"+v2);//V2:null
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴2=172, 迪丽热巴3=173}

}

/*remove(Object key) 返回v值
如果存在(从可选的操作),从该地图中删除一个键的映射。*/
private static void demo02() {
    Map<String ,Integer> map=new HashMap<>();
    map.put("迪丽热巴1",171 );
    map.put("迪丽热巴2",172 );
    map.put("迪丽热巴3",173 );
    map.put("迪丽热巴4",174 );
    System.out.println(map);
    Integer v = map.remove("迪丽热巴2");
    System.out.println("v:"+v);//v:172
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴3=173}
    Integer v1 = map.remove("迪丽热巴9");
    System.out.println("v1:"+v1);//v1:null
    System.out.println(map);//{迪丽热巴1=171, 迪丽热巴4=174, 迪丽热巴3=173}
}

/*put(K key, V value)返回v值
将指定的值与该映射中的指定键相关联(可选操作)。*/
private static void demo01() {
    Map<String ,String> map=new HashMap<>();
    String v1 = map.put("李晨", "范冰冰1");
    System.out.println("v1:"+v1);//v1:null,返回的map中key对应的v值
    String v2 = map.put("李晨", "范冰冰2");
    System.out.println("v2:"+v2);//v2:范冰冰1
    System.out.println(map);
    map.put("冷锋", "龙小云");
    map.put("李晨", "李小璐");
    map.put("李晨", "范冰冰");
    map.put("中国:", "龙小云");
    System.out.println(map);//{李晨=范冰冰, 中国:=龙小云, 冷锋=龙小云}key不重复,value可以重复!

}
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值