Java Map的概述和基本方法与遍历

map集合概述
    .Interface map <K,V>    k-->Key键的类型    V-->Value值的类型
    将键映射到值的对象,不能包含重复的键,每个键可以映射到最多一个值
    举例:学生的学号和姓名
    itheima001  林青霞
    itheima002  张曼玉
    itheima003  王祖贤

map集合的基本功能
    .V put (K key , V value);            添加元素
    .V remove(object key);                根据键删除键值对应的元素
    .void clear()                         移除所有的键值对元素
    .Boolean containsKey(object key);     判断集合中是否包含有指定的键
    .boolean containsvalue(object value); 判断集合中是否包含有指定的值
    .Boolean isEmpty();                   判断集合是否为空
    .int size();                          集合的长度,也就是集合中键值对的个数
    
map集合的获取功能 
    .V get(object key);                   根据键获取值
    .Set<K> KeySet();                     获取所有键的集合
    .collection<V>values()                获取所有值的集合
    .Set<Map.Entry<K,V>>entrySet();       获取所有键值对的集合

map集合的遍历

我们可以把maop集合看作为成对出现的夫妻对的集合

方式一:

 遍历思路:
     .把所有的丈夫集中起来
     .遍历丈夫的集合,获取到每一个丈夫
     .根据丈夫去找对应的妻子
     
转换为map集合中的操作
     .获取所有键的集合,,用keySet方法实现
     .遍历键的集合,获取到每一个键,用增强for实现
     .根据键去找值,用get(object Key)方法实现
     
方式二:

遍历思路:
     .获取所有结婚证的集合
     .遍历结婚证的集合,得到每一个结婚证
     .根据结婚证获取丈夫和妻子
     
转换为map集合中的操作:
     .获取所有键值对的集合
       Set<Map.Entry<K,V>>entrySet();获取所有键值对对象的集合
     .遍历键值对对象的集合,得到每一个键值对对象
       用增强for实现,得到每一个m.Entry
     .根据键值对对象获取键和值
      用getKey()得到键
      用getValue()得到值


 概述基本功能的代码

package Map集合;

import java.util.HashMap;
import java.util.Map;

public class map集合的基本功能1 {
	/*
map集合的基本功能
    .V put (K key , V value);            添加元素
    .V remove(object key);                根据键删除键值对应的元素
    .void clear()                         移除所有的键值对元素
    .Boolean containsKey(object key);     判断集合中是否包含有指定的键
    .boolean containsvalue(object value); 判断集合中是否包含有指定的值
    .Boolean isEmpty();                   判断集合是否为空
    .int size();                          集合的长度,也就是集合中键值对的个数
    
map集合的获取功能 
    .V get(object key);                   根据键获取值
    .Set<K> KeySet();                     获取所有键的集合
    .collection<V>values()                获取所有值的集合
    .Set<Map.Entry<K,V>>entrySet();       获取所有键值对的集合
    
	 */

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
//		Map<String,String> m = new HashMap<String,String>();
//		//添加元素
//		m.put("itheima001", "林青霞");
//		m.put("itheima002", "张曼玉");
//		m.put("itheima003", "王祖贤");
//		
//		//根据键删除键值对应的元素   返回值为对应键的值
//		System.out.println(m.remove("itheima002"));
//		
//		//判断集合中是否包含有指定的键
//		System.out.println(m.containsKey("itheima003"));
//		
//		//判断集合中是否包含有指定的值
//		System.out.println(m.containsValue("林青霞"));
//		
//		//判断集合是否为空
//		System.out.println(m.isEmpty());
//		
//		//集合的长度,也就是集合中键值对的个数
//		System.out.println(m.size());
//		
//		//移除所有的键值对元素
//		m.clear();
//		
//		System.out.println(m);
		
		Map<String,String> m = new HashMap<String,String>();
		m.put("itheima001", "林青霞");
		m.put("itheima002", "张曼玉");
		m.put("itheima003", "王祖贤");
		
		//根据键获取值
		System.out.println(m.get("itheima003"));
		
		//获取所有键的集合
		System.out.println(m.keySet());
		
		//获取所有值的集合
		System.out.println(m.values());
		
		//获取所有键值对的集合
		System.out.println(m.entrySet());

	}

}

 概述遍历的代码

package Map集合;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class map集合的遍历 {
/*
我们可以把maop集合看作为成对出现的夫妻对的集合

方式一:

 遍历思路:
     .把所有的丈夫集中起来
     .遍历丈夫的集合,获取到每一个丈夫
     .根据丈夫去找对应的妻子
     
转换为map集合中的操作
     .获取所有键的集合,,用keySet方法实现
     .遍历键的集合,获取到每一个键,用增强for实现
     .根据键去找值,用get(object Key)方法实现
     
方式二:

遍历思路:
     .获取所有结婚证的集合
     .遍历结婚证的集合,得到每一个结婚证
     .根据结婚证获取丈夫和妻子
     
转换为map集合中的操作:
     .获取所有键值对的集合
       Set<Map.Entry<K,V>>entrySet();获取所有键值对对象的集合
     .遍历键值对对象的集合,得到每一个键值对对象
       用增强for实现,得到每一个m.Entry
     .根据键值对对象获取键和值
      用getKey()得到键
      用getValue()得到值
 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		//方式一
//		Map<String,String> m = new HashMap<String,String>();
//		
//		//添加元素
//		m.put("itheima001", "林青霞");
//		m.put("itheima002", "张曼玉");
//		m.put("itheima003", "王祖贤");
//		
//		//获取所有键的集合,获取到每一个键,用增强for实现
//		Set<String> KeySet= m.keySet();
//		for (String i : KeySet) {
//			String value = m.get(i);
//			System.out.println(i+","+value);
//		}
		
		
		//方式二
		Map<String,String> m = new HashMap<String,String>();
		//添加元素
		m.put("itheima001", "林青霞");
		m.put("itheima002", "张曼玉");
		m.put("itheima003", "王祖贤");
		
		//获取所有键值对的集合
		Set<Map.Entry<String,String>> KeySet= m.entrySet();
		
		//遍历键值对对象的集合,得到每一个键值对对象
		for (Map.Entry<String, String> me : KeySet) {
			System.out.println(me.getKey()+","+me.getValue());
		}

	}

}

Hashmap存储学生对象并遍历升级版(案例)

1.student类

2.Hashmapdemo类

1.student类

package Hashmap存储学生对象并遍历升级版;

public class student {
	private String name;
	private int age;
	
	/**
	 * @return name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name 要设置的 name
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age 要设置的 age
	 */
	public void setAge(int age) {
		this.age = age;
	}
//-----------构造方法----------
	/**
	 * @param name
	 * @param age
	 */
	public student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
/**
 * 
 */
public student() {
	super();
}
@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;
}



}

2.Hashmapdemo类

 

package Hashmap存储学生对象并遍历升级版;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class hashdemo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
        Map<student,String> m = new HashMap<student,String>();
		
		student s = new student("林青霞",23);
		student s1 = new student("王祖贤",18);
		student s2 = new student("张曼玉",19);
		student s3 = new student("张曼玉",19);
		student s4 = new student("张曼玉",19);
		m.put(s,"成都");
		m.put(s1,"武汉");
		m.put(s2,"上海");
		m.put(s3,"北京");
		m.put(s4,"北京");
		 Set<Entry<student, String>> KeySet= m.entrySet();
			
			//遍历键值对对象的集合,得到每一个键值对对象
			for (Entry<student, String> me : KeySet) {
				System.out.println(me.getKey()+","+me.getValue());
			}

	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

想给世界留下 1bite 的印象

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

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

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

打赏作者

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

抵扣说明:

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

余额充值