java容器---Map集合的体系结构

    Map<Key,Value>集合的共性方法:

     K - 此映射所维护的键的类型
    V - 映射值的类型
    增
    put(K key, V value)
    putAll(Map<? extends K,? extends V> m)
    删
    remove(Object key)
    判断
    containsKey(Object key) 是否包含指定键
    containsValue(Object value) 是否包含指定值
    isEmpty()
    查询
    entrySet() 返回此映射中包含的映射关系的 Set 视图
    get(Object key)
    keySet() 返回此映射中包含的键的 Set 视图。

Map的常用子类:

  |---HashTable: 底层是Hash表数据结构,不允许存入null键null值。线程同步的,JDK1.1,效率低
  |---HashMap: 底层是Hash表数据结构,允许存入null键null值。线程不同步,JDK1.2,效率高(其余与HashTable基本相同)
  |---TreeMap:底层是二叉树数据结构,该映射根据其键的自然顺序进行排序。或者根据创建映射时提供的 Comparator 进行排序

              ,具体取决于使用的构造方法。

Map集合的两种遍历方式:

import java.util.*;
public class MapDemo{
	public static void main(String []args){
		Map<String,String> map = new HashMap<String,String>();
		map.put("01","lzl-1");
		map.put("02","lzl-2");
		map.put("03","lzl-3");
		map.put("04","lzl-4");
		map.put("05","lzl-4");
		method_keySet(map);
		method_entrySet(map);
	}
	public static void method_keySet(Map<String,String> map){
		//通过keySet方法获取Set<key>类型的值。
		Set<String> keySet = map.keySet();
		//有了Set集合,就可以用到Iterator(迭代器)了
		Iterator<String> it = keySet.iterator();
		while(it.hasNext()){
			//获取map集合中的key值
			String key = it.next();
			//通过Map集合中的get方法,获取Value值
			String value = map.get(key);
			System.out.println("keySet方法--->"+" key:"+key+" value:"+value);
		}
	}
	/*
	
	*/
	public static void method_entrySet(Map<String,String> map){
		//通过entrySet方法获取Map集合中的映射关系
		Set<Map.Entry<String,String>> entrySet = map.entrySet();
		//获取Set集合,使用迭代器提供的方法获取Map集合中存入的键值
		Iterator<Map.Entry<String,String>> it = entrySet.iterator();
		while(it.hasNext()){
			//Entry是Map接口这种的内部静态接口,实现该接口的方法,都拥有getValue()和getKey()的方法。
			Map.Entry<String,String> me = it.next();
			String value = me.getValue();
			String key = me.getKey();
			System.out.println("entrySet方法--->"+" key:"+key+" value:"+value);
		}
	}
}

例子:一个学生对象,包括姓名和年龄。每个学生都有家庭住址。

     我们认为学生的姓名和年龄相同表示同一个人。地址可以相同
     步骤:
     1、定义一个学生类,重写hashCode和equals方法。
        并实现Comparable接口,重写compareTo(Object o1,Object o2)方法按照年龄和姓名的顺序来排列
     2、将学生对象和学生的住址存入到HashMap中去。key--Student,value--String(地址)
     3、通过keySet或者entrySet方法将数据一一取出。
     解释:
     因为HashMap底层是hash表,每次new一个对象时,会产生不同的hashCode。因此不能比较Student中姓名和年龄相同的值。
     所以需要覆写hashCode和equals方法
     实现Comparable接口,为了规范,可以使TreeMap集合存储Student对象的数据。若不实现,则不能用TreeMap存储。

import java.util.*;
//实现Comparable接口,提供按照姓名和年龄的自然顺序排序的方法
class Student implements Comparable<Student>
{
	private String name;
	private int age;
	public Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	public int getAge(){
		return age;
	}
	public String getName(){
		return name;
	}
	public int compareTo(Student s){
		int num = this.getName().compareTo(s.getName());
		if(num == 0)
			return new Integer(this.getAge()).compareTo(new Integer(s.getAge()));
		return num;
	}
	public int hashCode(){
		//保证new出的对象的hash值的唯一性
		return name.hashCode()+age*60;
	}
	public boolean equals(Object obj){
		if(!(obj instanceof Student))
			throw new RuntimeException("类型不匹配");
		Student stu = (Student)obj;
		return this.age==stu.getAge() && this.name.equals(stu.getName());
	}
	public String toString(){
		return "[name = "+name+" age = "+age+"]";
	}
}
public class HashMapDemo{
	public static void main(String args[]){
		Map<Student,String> map = new HashMap<Student,String>();
		map.put(new Student("lzl",18),"河南理工");
		map.put(new Student("lzl1",18),"河南理工");
		map.put(new Student("lzl2",18),"河南理工");
		map.put(new Student("lzl3",18),"河南理工");
		map.put(new Student("lzl4",18),"河南理工");
		map.put(new Student("lzl4",18),"河南理工");
		getInfo(map);
	}
	public static void getInfo(Map<Student,String> map){
		Set<Map.Entry<Student,String>> entrySet = map.entrySet();
		Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
		while(it.hasNext()){
			Map.Entry<Student,String> me = it.next();
			Student stu = me.getKey();
			String address = me.getValue();
			System.out.println("Student:"+stu+" address:"+address);
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值