Map集合

1. Map集合概述和使用

概述

  • Interface Map<K, V> K:键的类型 V:值的类型
  • 将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值

创建Map集合对象

  • 多态的方式
  • 具体的实现类HashMap
  • 例子: Map<String, String> map=new HashMap<String, String>();

2.Map集合的基本功能

  • put(K key, V value); 添加元素
  • remove(Object key); 根据键删除键值对元素
  • clear(); 移除所有的键值对元素
  • boolean containsKey(Object value); 判断集合是否包含指定的键
  • boolean containsValue(Object value); 判断集合是否包含指定的值
  • boolean isEmpty();判断集合是否为空
  • int size(); 集合的长度,即集合中键值对的个数

3. Map集合的获取功能

  • get(Object key); 根据键获取值
  • Set keySet();获取所有键的集合
  • Collection values(); 获取所有值的集合
  • Set<Map.Entry<K,V>> entrySet(); 获取所有键值对对象的集合
Set<String> keyset=map.keySet();
	for(String i:keyset)
		System.out.println(i);
	System.out.println("------------------------");
	Collection<String> value=map.values();
	for(String j:value)
		System.out.println(j);
	System.out.println("------------------------");
	Set<Entry<String, String>> en=map.entrySet();
	for(Entry<String, String> k:en)
	System.out.println(k);

4.Map集合的遍历

方法一

  • 获取所有键的集合,用keySet()方法实现
  • 遍历键的集合,获取到每一个键。用增强for实现
  • 根据键去找值。用get(Object key)方法实现
Set<String> keySet = map.keySet();
for(String i:keySet) {
	String value=map.get(i);
	System.out.println(i+","+value);
}

方法二(推荐)

  • 获取所有键值对对象的集合,用Set<Map.Entry<K,V>> entrySet()实现

  • 遍历键值对对象的集合,得到每一个键值对对象,用增强for实现,得到每一个Map.Entry

  • 根据键值对对象获取键和值

    用getKey()得到键

    用getValue()得到值

    Set<Entry<String, String>> entrySet = map.entrySet();
    for(Entry<String, String> i:entrySet)
    {
    	String key=i.getKey();
    	String value=i.getValue();
    	System.out.println(key+","+value);
    }
    

    5.案例

    1.创建一个HashMap集合,键是学号(String),值是学生对象(Student)。存储三个键值对元素,并遍历。

    public static void main(String[] args) {
    	Map<String,Student> map=new HashMap<String, Student>();
    	Student student1 = new Student("Anna",18);
    	Student student2 = new Student("Bob",20);
    	Student student3 = new Student("Cathy",17);
    	map.put("001", student1);
    	map.put("002", student2);
    	map.put("003", student3);
    	Set<Entry<String, Student>> entrySet = map.entrySet();
    	for(Entry<String, Student> i:entrySet) {
    		String number=i.getKey();
    	Student student=i.getValue();
    	String name=student.getName();
    	int age=student.getAge();
    		System.out.println(number+":"+name+","+age);
    	}
    }
    

    2.创建一个HashMap集合,键是学生对象(Student),值是居住地(String)。存储多个键值对元素,并遍历。要求保证键的唯一性:如果学生对象的成员变量值相同,我们就认定是同一个对象

public static void main(String[] args) {
	Map<Student, String> map = new HashMap<Student,String>();
	Student student1 = new Student("Anna",18);
	Student student2 = new Student("Bob",17);
	Student student3 = new Student("Cathy",20);
	Student student4 = new Student("Cathy",20);
	map.put(student1, "Shanghai");
	map.put(student2, "Beijing");
	map.put(student3, "Changsha");
	map.put(student4, "hangzhou");//会覆盖上一条
	Set<Entry<Student, String>> entrySet = map.entrySet();
	for(Entry<Student, String> i:entrySet) {
		Student key=i.getKey();
		String value=i.getValue();
		System.out.println(key.getName()+","+key.getAge()+","+value);
	}
}

3.创建一个ArrayList集合,存储三个元素,每一个元素都是HashMap,每一个HashMap的键和值都是String类型的,并遍历集合。

public static void main(String[] args) {
	ArrayList<HashMap<String,String>> arrayList = new ArrayList<HashMap<String,String>>();
	HashMap<String, String> hashMap1 = new HashMap<String, String>();
	hashMap1.put("011", "one");
	hashMap1.put("012", "two");
	hashMap1.put("013", "three");
	arrayList.add(hashMap1);
	HashMap<String, String> hashMap2 = new HashMap<String, String>();
	hashMap1.put("021", "Monday");
	hashMap1.put("022", "Tuesday");
	hashMap1.put("023", "Wednesday");
	arrayList.add(hashMap2);//输出为无序的
	for(HashMap<String, String> i:arrayList) {
		
		
		  Set<Entry<String, String>> entrySet = i.entrySet(); for(Entry<String,String>
		  j:entrySet) { System.out.println(j.getKey()+","+j.getValue());
		  
		  }
		 
		/*
		 * Set<String> keySet = i.keySet(); for(String j:keySet) {
		 * 
		 * System.out.println(j+","+i.get(j)); }
		 */
		
	}
}

4.创建一个HashMap集合,存储三个键值对元素,每一个键值对元素的键是String,值是ArrayList,每一个ArrayList的元素是String,并遍历。

public static void main(String[] args) {
	HashMap<String,ArrayList<String>> hashMap = new HashMap<String, ArrayList<String>>();
	ArrayList<String> array1 = new ArrayList<String>();
	array1.add("hello");
	array1.add("world");
	array1.add("java");
	hashMap.put("001", array1);
	
	ArrayList<String> array2 = new ArrayList<String>();
	array2.add("hello");
	array2.add("world");
	array2.add("c");
	hashMap.put("002", array2);
	ArrayList<String> array3 = new ArrayList<String>();
	array3.add("hello");
	array3.add("world");
	array3.add("c++");
	hashMap.put("003", array3);
	
	Set<Entry<String, ArrayList<String>>> entrySet = hashMap.entrySet();
	
	for(Entry<String, ArrayList<String>> i:entrySet)
	{
		String key=i.getKey();
		ArrayList<String> value=i.getValue();
		for(String j:value) {
			System.out.println(key+","+j);
		}
	}
}

5.键盘录入一个字符串,要求统计字符串中每个字符串出现的次数。

public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	System.out.println("请输入一个字符串");
	String s=input.next();
	HashMap<Character, Integer> hashMap = new HashMap<Character,Integer>();
	char[] c=s.toCharArray();
	for(char i:c) {
		Integer value = hashMap.get(i);
		if(value==null)
			hashMap.put(i, 1);
		else {
			value++;
			hashMap.put(i, value);
		}
	}
	Set<Entry<Character, Integer>> entrySet = hashMap.entrySet();
	for(Entry<Character,Integer> i:entrySet)
	{
		System.out.println(i.getKey()+"("+i.getValue()+")");
	}
}

TreeMap对元素进行排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容与0801

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值