Map接口

Map

为什么需要map集合?

  • 学生id 姓名 年龄 成绩

  • 2018050401 张三 18 80.0

  • 2018050402 李四 20 85.0

  • 2018050403 李四 21 89.0

  • 2018050404 王五 21 89.0

  • 如果使用已经学习过的知识点,如何来存储如上的数据?

  • HashSet idList LinkedHashSet

  • ArrayList nameList

  • ArrayList ageList

  • ArrayList scoreList

  • 需求: 请通过学号查询某个学生的学生信息?

  • 提供一个学号2018050402,可以通过学号来获取学生姓名吗?

  • 所以针对这种情况,Java就设计Map集合

  • Map集合提供了集合之间一种映射关系

  • 让集合和集合之间产生关系

  • Map<String,Student>

  • Map<String,Double>

  • Map集合的特点

  • 1.能够存储唯一的列的数据(唯一,不可重复) Set

  • 2.能够存储可以重复的数据(可重复) Collection

  • 3.值的顺序取决于键的顺序

  • 4.键和值都是可以存储null元素的

  • 一个映射不能包含重复的键。

  • 每个键最多只能映射到一个值。

  • 常用功能

  • 1.添加功能

  •  V put(K key, V value)  
    
  •  void putAll(Map<? extends K,? extends V> m) 
    
  • 2.删除功能

  •  V remove(Object key) 
    
  •  void clear() 
    
  • 3.遍历功能

  •  Set<K> keySet() 
    
  •  Collection<V> values()
    
  •  Set<Map.Entry<K,V>> entrySet() 
    
  • 4.获取功能

    V get(Object key)

    5.判断功能

    boolean containsKey(Object key)

    boolean containsValue(Object value)

    boolean isEmpty()

    6.修改功能

    V put(K key, V value)

    void putAll(Map<? extends K,? extends V> m)

    7.长度功能

    int size()

    注意:

    1.键相同表示修改,键不同表示添加

    2.第一次添加元素,返回结果为null,第二次添加返回上一次的键对应的值,返回 “前任”;

    3.键是唯一的,无序的,类似于Set接口,值是可重复的,顺序取决于键的顺序,类似于Collection

    4.并且键和值存在映射关系,建立了集合之间的联系

public class MapDemo01 {
	public static void main(String[] args) {
		Map<String, String> map = new HashMap<>();
		System.out.println(map); // {} 类似于 json格式的数据
		System.out.println("V put(K key, V value):" + map.put("琦玉", "龙卷")); // null
		System.out.println(map);
		System.out.println("V put(K key, V value):" + map.put("琦玉", "吹雪")); // 龙卷
		System.out.println(map);
		
		map.put("柯南", "灰原哀");
		map.put("路飞", "女帝");
		map.put("鸣人", "向日雏田");
		map.put("路人甲", "灰原哀");
		
		System.out.println(map);
		
		Map<String, String> map2 = new HashMap<>();
		map2.put("西门庆", "潘金莲");
		map2.put("猪八戒", "嫦娥");
		map.putAll(map2);
		System.out.println(map);
		
		System.out.println(map.get("路人甲"));
		
		System.out.println(map.containsKey("琦玉"));
		System.out.println(map.containsValue("灰原哀"));
		
		System.out.println(map.remove("路飞"));
		System.out.println(map);
		
		// map.clear();
		// System.out.println(map);
		System.out.println(map.size());
	}
}

Map接口的遍历

  • Set keySet() 遍历键
  • Collection values()
  • Set<Map.Entry<K,V>> entrySet() //<Map.Entry<K,V>>里面是每个元素是Map,还是Map的Entry<K,V>静态接口
public class MapDemo02 {
	public static void main(String[] args) {
		Map<String, String> map = new HashMap<>();
		
		map.put("柯南", "灰原哀");
		map.put("路飞", "女帝");
		map.put("鸣人", "向日雏田");
		map.put("路人甲", "灰原哀");
		System.out.println(map);
		
		// 遍历键
		Set<String> keys = map.keySet();
		for (String key : keys) {
			System.out.println(key);
		}
		System.out.println("================");
		// 遍历值
		Collection<String> values = map.values();
		for (String value : values) {
			System.out.println(value);
		}
		// 遍历键和值
		// 四种方式
		// 方式一: 
		/*
		 * 1.获取到键的集合
		 * 2.遍历键的集合获取到每一个键
		 * 3.通过键获取值
		 * 4.输出键和值
		 */
		System.out.println("================");
		for (String key : map.keySet()) System.out.println(key + "=" + map.get(key));
		System.out.println("================");
		// 方式二: 
		// Iterator<String> it = map.keySet().iterator();
		Iterator<String> it = map.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			String value = map.get(key);
			System.out.println(key + "=" + value);
		}
		System.out.println("================");
		// 方式三:
		/*
		 * 1.获取 键值对 整体的集合
		 * 2.遍历 键值对对象的集合 ,获取到每一对键值对 Entry<String, String>
		 * 3.通过每一对键值对对象 分别 获取键 获取值
		 * 4.输出即可
		 */
        // Set<Entry<String,String>> keyValues = map.entrySet();
		for (Entry<String, String> keyValue : map.entrySet()) 
        // String key = keyValue.getKeyt();
		//String value = keyValue.getValue(); 
        System.out.println(keyValue.getKey() + "=" + keyValue.getValue());
		System.out.println("================");
		// 方式四:
		Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<String, String> keyValue = iterator.next();
			System.out.println(keyValue.getKey() + "=" + keyValue.getValue());
		}
		System.out.println("================");
		// 观察JDK的源码遍历方式
		System.out.println(map);
		/*
		 * public String toString() {
		        Iterator<Entry<K,V>> i = this.entrySet().iterator();
		        if (! i.hasNext())
		            return "{}";
		
		        StringBuilder sb = new StringBuilder();
		        sb.append('{');
		        for (;;) {
		            Entry<K,V> e = i.next();
		            K key = e.getKey();
		            V value = e.getValue();
		            sb.append(key   == this ? "(this Map)" : key);
		            sb.append('=');
		            sb.append(value == this ? "(this Map)" : value);
		            if (! i.hasNext())
		                return sb.append('}').toString();
		            sb.append(',').append(' ');
		        }
		    }
		 */
	}
}

课堂案例

/*
 * 1、使用HashMap类实例化一个Map类型的对象m1,键(String类型)和值(Integer型)分别      用于存储员工的姓名和工资,存入数据如下:
            张三——800元;李四——1500元;王五——3000元;
	1)将张三的工资更改为2600元
	2)为所有员工工资加薪100元;
	3)遍历集合中所有的员工
	4)遍历集合中所有的工资
	试试将键改写成Employee
 */
public class MapDemo03 {
	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("张三", 800);
		map.put("张三", 200);
		map.put("李四", 1500);
		map.put("王五", 3000);
		
		System.out.println(map);
		// 1)将张三的工资更改为2600元
		map.put("张三", 2600);
		System.out.println(map);
		
		// 2)为所有员工工资加薪100元;
		for(String key : map.keySet()) {
			Integer value = map.get(key);
			map.put(key, value + 100);
		}
		System.out.println(map);
		
		// 3)遍历集合中所有的员工
		for(String key: map.keySet()) {
			System.out.println(key);
		}
		
		// 4)遍历集合中所有的工资
		for(Integer salary: map.values()) {
			System.out.println(salary);
		}
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值