黑马程序员------集合(No.1)(Map集合、Map共性方法、keySet、entrySet)

---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ---------------------- 

 

微笑Map集合

 

Map集合:该集合存储键值对。一对一往里存。而且要保证键 的唯一性。

1.添加:

put(K key ,V value)

putAlll(Map<? extends K,?extends V> m)

 

2.删除:

clear();

remove(Object key);

 

3.判断:

containsValue(Object value)

containKey(Object key)

isEmpty()

 

4.获取:

get(Object key)

size()

values();

entrySet();

keySet();

 

Map

  |----Hashtable:底层是哈希表数据结构,不可以存入null键和null值。该集合是线程同步的。JDK1.0,效率低。

  |----HashMap:底层是哈希表数据结构,允许使用null键和null值,该集合是不同步的。JDK1.2,效率高。

  |----TreeMap:底层是二叉树数据结构。线程不同步,可以用于给map集合中的键进行排序。

 

和Set很像,其实,Set的底层就是使用了Map集合。

Map共性方法

import java.util.*;
class MapDemo{
	public static void main(String[] args){
		Map<String,String> map = new HashMap<String,String>();
		//添加元素,如果出现添加时,相同的键,后添加的值会覆盖原有的值,并且该方法会返回被覆盖的值
		map.put("01","zhangsan1");
		map.put("02","zhangsan2");
		map.put("03","zhangsan3");
		map.put("04","zhangsan4");
		System.out.println("containsKey:"+map.containsKey("002"));
		//System.out.println("remove:"+map.remove("02"));
		System.out.println("get:"+map.get("023"));
		//HashTable可以通过get方法的返回值来判断一个键是否存在。通过返回null来判定
		//特殊情况。
		map.put("05",null);
		System.out.println("get:"+map.get("05"));
		//获取map集合中所有的值
		Collection<String> coll = map.values();
		System.out.println(coll);
		System.out.println(map);
	}
}


 通过迭代方法得到元素。

import java.util.*;
class MapDemo2{
	public static void main(String[] args){
		Map<String,String> map = new HashMap<String,String>();
		map.put("01","zhangsan1");
		map.put("02","zhangsan2");
		map.put("03","zhangsan3");
		map.put("04","zhangsan4");
		System.out.println(map);
		//Map.Entry其实Entry也是一个接口,它是Map接口的一个内部接口。
		Set<Map.Entry<String,String>> entrySet = map.entrySet();
		Iterator<Map.Entry<String,String>> it = entrySet.iterator();
		while(it.hasNext()){
			Map.Entry<String,String> me = it.next();
			System.out.println(me.getKey()+":"+me.getValue());
		}
	}
}


 需求:对学生对象的姓名进行自然排序。

因为数据是以键值对的形式存在的。

所以要使用可以排序的Map集合。TreeMap。

 

 有两种方式,均在下面的程序中体现。

 

import java.util.*;
/*
 需求:对学生对象的姓名进行自然排序。
因为数据是以键值对的形式存在的。所以要使用可以排序的Map集合。TreeMap。
*/
class Student implements Comparable<Student>{
	private String name;
	private int age;
	Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	public void setAge(int age){
		this.age=age;
	}
	public int getAge(){
		return age;
	}
	//重写hashCode方法
	public int hashCode(){
		return name.hashCode()+age*15;
	}
	//重写equals方法
	public boolean equals(Object obj){
		if(!(obj instanceof Student))
			throw new ClassCastException("类型不匹配");
		Student s = (Student)obj;
		return this.getName().equals(s.getName())&&this.getAge()==s.getAge();
	}
	//重写compareTo方法
	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;
	}
	//重写toString方法
	public String toString(){
		return "["+this.getName()+":"+this.getAge()+"]";
	}
}
class MapDemo3{
	public static void main(String[] args){
		//使用TreeMap要按顺序,方法一需要传入实现Comparator接口的子类
		//TreeMap<Student,String> hm = new TreeMap<Student,String>(new MyComp());
		//使用TreeMap要按顺序,方法二需要传入Student实现Comparable接口。
		TreeMap<Student,String> hm = new TreeMap<Student,String>();
		hm.put(new Student("xiaozi5",12),"beijng1");
		hm.put(new Student("xiaozi7",13),"beijng3");
		hm.put(new Student("xiaozi2",15),"beijng2");
		hm.put(new Student("xiaozi3",16),"beijng4");
		hm.put(new Student("xiaozi2",11),"beijng5");
		
	//	System.out.println(hm);
		Set<Map.Entry<Student,String>> hs = hm.entrySet();
		Iterator<Map.Entry<Student,String>> it = hs.iterator();
		while(it.hasNext()){
			Map.Entry<Student,String> me = it.next();
			System.out.println(me);
		}
	}
}
class MyComp implements Comparator<Student>{
	public int compare(Student s1,Student s2){
		int num = s1.getName().compareTo(s2.getName());
		if (num==0)
		{
			return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
		}
		return num;
	}
}


---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ---------------------- 

详情请查看:http://edu.csdn.net

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值