Map接口常用方法总结(Java: 集合与泛型(三))

4 篇文章 0 订阅
2 篇文章 0 订阅

一、Map接口概述:

  1. Map集合基于键(key)/值(value)映射。每个键最多只能映射一个值。键可以是任何引用 数据类型的值,不可重复;值可以是任何引用数据类型的值,可以重复;键值对存放无序。
  2. Map常用实现类有:HashMap, LinkedHashMap和Properties.
  3. Map不可以直接实例化,需要通过其实现类进行实例化.

二、Map接口常用方法总结:

注:该博客代码中引包代码均省略,eclipse用户可通过CTRL+shift+o来进行快捷引包

  1. put(K key, V value) 将键(key)/值(value)映射存放到Map集合中
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);//此时该HashMap中存储了"Tom"-91这一组键值对
	}
}
  1. get(Object key) 返回指定键所映射的值,没有该key对应的值则返回 null
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		int score = scores.get("Tom");
		System.out.println(score);//此处输出为91
	}
}
  1. size() 返回Map集合中数据数量
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		int numbers = scores.size();
		System.out.println(numbers);//此处输出为3
	}
}
  1. clear() 清空Map集合
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		scores.clear();
		int numbers = scores.size();
		System.out.println(numbers);//此处输出为0
	}
}
  1. isEmpty () 判断Map集合中是否有数据,如果没有则返回true,否则返回false
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		System.out.println(scores.isEmpty());//此处输出为false
		scores.clear();
		System.out.println(scores.isEmpty());//此处输出为ture
		
	}
}
  1. remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值。
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		System.out.println(scores.remove("Tom"));//此处输出91
		System.out.println(scores.get("Tom"));//此处输出为null
	}
}
  1. values() 返回Map集合中所有value组成的以Collection数据类型格式数据。 eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		System.out.println(scores.values());//此处输出为[91, 89, 95]
	}
}
  1. containsKey(Object key) 判断集合中是否包含指定键,包含返回 true,否则返回false
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		System.out.println(scores.containsKey("Tom"));//此处输出ture
		System.out.println(scores.containsKey("tom"));//此处输出false
	}
}
  1. containsValue(Object value) 判断集合中是否包含指定值,包含返回 true,否则返回false
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		System.out.println(scores.containsValue(91));//此处输出为ture
		System.out.println(scores.containsValue(100));//此处输出为false	
	}
}
  1. keySet() 返回Map集合中所有key组成的Set集合
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		System.out.println(scores.keySet());//输出为[Tom, Jack, Jim]
	}
}
  1. entrySet() 将Map集合每个key-value转换为一个Entry对象并返回由所有的Entry对象组成的Set 集合
    eg:
public class Main {
	public static void main(String[] args) {
		Map<String, Integer> scores = new HashMap<String, Integer>();
		scores.put("Tom", 91);
		scores.put("Jim", 89);
		scores.put("Jack", 95);
		System.out.println(scores.entrySet());//输出为:[Tom=91, Jack=95, Jim=89]
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值