Map集合

一、 Map集合概述和特点
Map接口概述
将键映射到值的对象
一个映射不能包含重复的键
每个键最多只能映射到一个值
Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构(TreeMap,hashMap)值针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
二、 Map集合的功能概述
1. 添加功能

V put(K key,V value):添加元素。
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

Map<String, Integer> map =new HashMap<String, Integer>();
		//put的返回值是根据后面的值的类型相同
		Integer i1= map.put("张三", 13);
		Integer i2= map.put("李四", 15);
		Integer i3= map.put("王五", 14);
		Integer i4= map.put("赵六", 16);
		Integer i5= map.put("张三", 16);	
//因为map集合种相同的键不存储,值覆盖,把被覆盖的值返回
		System.out.println(map);
		System.out.println(i1);
		System.out.println(i2);
		System.out.println(i3);
		System.out.println(i4);
		System.out.println(i5);

效果如下:
在这里插入图片描述

2. 删除功能

void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回

Map<String, Integer> map =new HashMap<String, Integer>();
		map.put("张三", 13);
		map.put("李四", 15);
		map.put("王五", 14);
		map.put("赵六", 16);
		//根据键删除元素,返回键对应的值
		Integer value =map.remove("张三");
		System.out.println(value);
		System.out.println(map);

效果如下:
在这里插入图片描述
3. 判断功能

boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空

		System.out.println(map.containsKey("张三"));
		System.out.println(map.containsValue(100));

返回值 一个true。一个false
4. 获取功能

Set<Map.Entry<K,V>> entrySet():
V get(Object key):根据键获取值
Set keySet():获取集合中所有键的集合
Collection values():获取集合中所有值的集合
前三个看后面的遍历。这里介绍最后一个

Map<String, Integer> map =new HashMap<String, Integer>();
		map.put("张三", 13);
		map.put("李四", 15);
		map.put("王五", 14);
		map.put("赵六", 16);
		
		Collection<Integer>collection =map.values();
		System.out.println(collection);

在这里插入图片描述
5. 长度功能

int size():返回集合中的键值对的个数

System.out.println(map.size());

三、 Map集合的遍历之键找值
主要介绍map的获取功能
键找值思路:

获取所有键的集合
遍历键的集合,获取到每一个键
根据键找值
方式一:使用迭代器遍历,

Map<String, Integer> map =new HashMap<String, Integer>();
		map.put("张三", 13);
		map.put("李四", 15);
		map.put("王五", 14);
		map.put("赵六", 16);
		
		Integer i =map.get("张三");		//根据键获取值
		System.out.println(i);
		
		Set<String> keySet =map.keySet();	//获取所有键的集合
		Iterator<String> it =keySet.iterator();//获取迭代器
		while (it.hasNext()) {					
		//判断集合中是否有元素
			String key = (String) it.next();	//获取每一个键
			Integer value =map.get(key);		//根据键获取值
			System.out.println(key+"=="+value);
		}

方式二:foreach循环

Map<String, Integer> map =new HashMap<String, Integer>();
		map.put("张三", 13);
		map.put("李四", 15);
		map.put("王五", 14);
		map.put("赵六", 16);
		 for (String key : map.keySet()) {		//map.keySet()是所有键的集合
			System.out.println(key+"=="+map.get(key));
		}
	}

在这里插入图片描述
四、 Map集合的遍历之键值对对象找键和值
键值对对象找键和值思路:

获取所有键值对对象的集合
遍历键值对对象的集合,获取到每一个键值对对象
根据键值对对象找键和值
先解释Map.Entry<K,V>是什么意思

interface Inter{
	interface Inter2{
		public void show();
	}
}
//想要实现Inter2接口需要使用Inter.Inter2
class Demo implements Inter.Inter2{
	@Override
	public void show() {

	}
}

同理:Entry是map下的一个子接口
方式一:根据迭代器查找

Map<String, Integer> map =new HashMap<String, Integer>();
		map.put("张三", 13);
		map.put("李四", 15);
		map.put("王五", 14);
		map.put("赵六", 16);
		//Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中
Set<Map.Entry<String, Integer>>entrySet =map.entrySet();
		//获取每一个对象
Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();
		while (it.hasNext()) {
			//获取每一个Entry对象
		Map.Entry<String, Integer> en =it.next();//父类引用指向子类对象
	// Entry<String ,Integer> en=it.next;	//直接获取的是子类对象。Entry是map.Entry的子接口
	//以上两种方式均可
		String key =en.getKey();			
//根据键值对对象获取键											
		Integer value =en.getValue();		
//根据键值对对象获取值
			System.out.println(key+"==="+value);
		}
	}

方式二:foreach循环遍历

Map<String, Integer> map =new HashMap<String, Integer>();
		map.put("张三", 13);
		map.put("李四", 15);
		map.put("王五", 14);
		map.put("赵六", 16);
		for (Map.Entry<String, Integer> en : map.entrySet()) {
	System.out.println(en.getKey()+"=="+en.getValue());
		}

在这里插入图片描述
五、 HashMap集合键是Student值是String的案例

public class Student {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student() {
		super();
	}
	public Student(String name, int age) {
		
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}

该处我们重写了hashCode方法和equals方法。所以当我们的姓名和年龄相同的时候视为同一对象。会对其进行覆盖。
测试类:

		//键表示字符串对象,值代表归属地
		HashMap<Student, String > hm =new HashMap<Student, String>();
		hm.put(new Student("张三",12), "安徽");
		hm.put(new Student("张三",12), "上海");
		hm.put(new Student("王五",11), "广州");
		hm.put(new Student("赵六",14), "深圳");
		
		System.out.println(hm);

在这里插入图片描述
六、 LinkedHashMap的概述和使用
LinkedHashMap的特点

  • 底层是链表实现的可以保证怎么存就怎么取
LinkedHashMap<String, Integer> lhm =new LinkedHashMap<String, Integer>();
		lhm.put("张三",13);
		lhm.put("李四",14);
		lhm.put("赵六",12);
		lhm.put("王五",17);
		System.out.println(lhm);

在这里插入图片描述
七、 TreeMap集合键是Student值是String的案例

TreeMap<Student, String> tm =new TreeMap<Student, String>();
		tm.put(new Student("张三", 13),"安徽");
		tm.put(new Student("李四", 12),"安徽");
		tm.put(new Student("王五", 14),"安徽");
		tm.put(new Student("赵六", 15),"安徽");
		
		System.out.println(tm);

**此时我们应当重写Comparable接口。我们按照年龄进行排序

**

@Override
	public int compareTo(Student o) {
		int num =this.age -o.age;
		
		return num==0? this.name.compareTo(o.name):num;
	}

在这里插入图片描述
用调用比较器Compartor

TreeMap<Student, String> tm =new TreeMap<Student, String>(new Comparator<Student>() {
			@Override
			public int compare(Student s1, Student s2) {
				int num =s1.getName().compareTo(s2.getName());
				return num ==0?s1.getAge()-s2.getAge():num;
			}
		});
		tm.put(new Student("张三", 13),"安徽");
		tm.put(new Student("李四", 12),"安徽");
		tm.put(new Student("王五", 14),"安徽");
		tm.put(new Student("赵六", 15),"安徽");

在这里插入图片描述
八、 练习比如:aaabbbbcccc。计算a出现次数b出现次数。
分析:

  • 定义一个需要被统计字符的字符串
  • 将字符串转换为字符数组
  • 定义双列集合,存储字符串中字符以及字符出现的次数
  • 遍历字符数组获取每一个字符并将字符存储在双列集合中
  • 存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储如果集合中包含这个键,就将值+1存储
  • 打印双列集合,获取字符出现的次数:统计字符串中每个字符出现的次数
public class Demo1_Test {
	public static void main(String[] args) {
		//1,定义一个需要被统计字符的字符串
		String s="aaaabbbbccccc";
		//2.将字符串转换为字符数组
		char[] arr =s.toCharArray();
		//定义双列集合存储字符串中字符以及字符出现的次数。HashMap效率最高
		HashMap<Character, Integer> hm =new HashMap<Character, Integer>();
		//遍历字符数组获取每一个字符,并将字符存储在双列集合中
		for (char c : arr) {
			//
			//if (!hm.containsKey(c)) {
			//	hm.put(c, 1);
			//}else {
			//	hm.put(c,hm.get(c)+1);
			//}
			hm.put(c,!hm.containsKey(c)?1:hm.get(c)+1);
		}
		for (Character key : hm.keySet()) {
	//hm.keySet代表所有键的集合
			System.out.println(key+"="+hm.get(key));
//根据键获取值
		}
	}

}

九、 集合嵌套之HashMap嵌套HashMap

//定义第一个集合
		HashMap<Student, String> hm1 =new HashMap<Student, String>();
		hm1.put(new Student("张三", 13),"安徽");
		hm1.put(new Student("李四", 15),"浙江");
		hm1.put(new Student("王五", 14),"河南");
		hm1.put(new Student("赵六", 12),"河北");
		
		//定义第二个集合
		HashMap<Student, String> hm2 =new HashMap<Student, String>();
		hm2.put(new Student("戚明辉", 16),"上海");
		hm2.put(new Student("江杰", 19),"北京");
		hm2.put(new Student("虞俊文", 18),"广州");
		hm2.put(new Student("宋进锋", 17),"深圳");
		
		//定义一个总的集合
		HashMap<HashMap<Student, String>, String> hm3 =new HashMap<HashMap<Student,String>, String>();
		hm3.put(hm1, "集合1");
		hm3.put(hm2, "集合2");
		
		//遍历双列集合
		for (HashMap<Student, String> h : hm3.keySet()) {	//hm3.keySet()代表的是双列集合中键的集合
			String value =hm3.get(h);		//get(h)根据键对象获取值对象
			//遍历键的双列集合对象
			for (Student key : h.keySet()) {//h.keySet获取集合中所有的学生键对象
				String value2=h.get(key);
				
				System.out.println(key+"=="+value2+"=="+value);
			}
		}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值