Map

转载出处:http://m.blog.csdn.net/article/details?id=44153345


五.Map


1.Map集合:该集合存储键值对。一对一往里存。而且要
保证键的唯一性
Map中的存储的一对元素:一个是键,一个是值,键与值之间有对应(映射)关系。

2.Map集合的常用方法:
1)添加:
put(K key,V value):添加元素。当存储的键相同时,新的值会替换老的值,并将老值返回。如果键没有重复,返回null。
putAll(Map):添加一个集合。

2)删除:
clear():清空。
remove(key) :删除指定键值对。

3)判断:
containsValue(value):判断值是否存在。
containsKey(key):判断键是否存在。
isEmpty():判断是否为空。

4)获取:
get(key):通过键获取对应的值。(注意:可以通过get方法的返回值来判断一个键是否存在,通过返回null来判断
size():获取集合的长度。
values():获取集合中所有的值。返回值类型是集合Collection。
keySet():
entrySet():

3.Map分类(Map集合的子类):
1)Hashtable:底层是哈希表数据结构,是线程同步的。效率低。不可以存储null键,null值。
2)HashMap:底层是哈希表数据结构,是线程不同步的。效率高。可以存储null键,null值。替代了Hashtable.
3)TreeMap:底层是二叉树结构,可以对map集合中的键进行指定顺序的排序。
Map和Set很像。其实Set底层就是使用了Map集合。

4.Map集合的两种取出方式:
1)Set<k> keySet:将Map集合中所有的键存入到Set集合。
因为Set具备迭代器。
所以可以通过迭代方式取出所有的键,再根据get方法,获取每一个键对应的值。
Map集合的取出原理:将Map集合转成Set集合,再通过迭代器取出。
(代码 MapDemo.java):

import java.util.*;
class MapDemo 
{
	public static void main(String[] args) 
	{
		Map<String,String> map = new HashMap<String,String>();
		map.put("03","zs3");
		map.put("01","zs1");
		map.put("04","zs4");
		map.put("02","zs2");

        //先获取Map集合的所有键的Set集合,keySet();
		Set<String> keySet = map.keySet();

		//有了Set集合,就可以获取其迭代器。
		Iterator<String> it = keySet.iterator();
		while(it.hasNext())
		{
			String key = it.next();
			//有了键就可以通过Map集合的get方法获取其对应的值。
			String value = map.get(key);
			System.out.println("key="+key+",value="+value);
		}
	}
}


2)Set<Map.Entry<k,v>> entrySet:将Map集合中的映射关系存入到了Set集合中,而这个关系的数据类型就是:Map.Entry。
(代码 MapDemo2.java):

import java.util.*;
class MapDemo2 
{
	public static void main(String[] args) 
	{
		Map<String,String> map = new HashMap<String,String>();
		map.put("03","zs3");
		map.put("01","zs1");
		map.put("04","zs4");
		map.put("02","zs2");

		//将Map集合中的映射关系取出,存入到Set集合中。
		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();
			String key = me.getKey();
			String value = me.getValue();
			System.out.println(key+"::"+value);
		}
	}
}


5.练习:
(代码 MapTest.java):

/*
练习:
每一个学生都有对应的归属地。
学生Student,地址String。
学生属性:姓名,年龄。
注意:姓名和年龄相同的视为同一个学生。
保证学生的唯一性。
*/
/*
步骤:
1,描述学生。
2,定义Map容器,将学生作为键,地址作为值,存入。
3,获取Map集合中的元素。
*/
import java.util.*;

class Student implements Comparable<Student>
{
	private String name;
	private int age;
	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	public int hashCode()
	{
		return name.hashCode()+age*34;
	}
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
			throw new ClassCastException("类型不匹配");
		Student s = (Student)obj;
		return this.name.equals(s.name) && this.age==s.age;
	}
    public int compareTo(Student c)
	{
		int num = new Integer(this.age).compareTo(new Integer(c.age));
		if(num==0)
			return this.name.compareTo(c.name);
		return num;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
	//复写toString,自定义输出内容.
	public String toString()
	{
		return name+"::"+age;
	}
} 

class MapTest 
{
	public static void main(String[] args) 
	{
		HashMap<Student,String > hm = new HashMap<Student,String >();  
        hm.put(new Student("lisi",12),"beijing");  
        hm.put(new Student("lisi",11),"shanghai");  
        hm.put(new Student("lisi",10),"tianjin");  
        hm.put(new Student("lisi",14),"nanjing");  
        hm.put(new Student("lisi",13),"wuhan");  

		//第一种取出方式keySet
		Set<Student> keySet = hm.keySet();
		Iterator<Student> it = keySet.iterator();
        while(it.hasNext())
		{
			Student stu = it.next();
			String addr = hm.get(stu);
			System.out.println(stu+"..."+addr);
		}

		//第二种取出方法Map.Entry
		Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
		Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();
		while(iter.hasNext())
		{
			Map.Entry<Student,String> me = iter.next();
			Student stu = me.getKey();
			String addr = me.getValue();
			System.out.println(stu+"......"+addr);
		}
		
	}
}


(代码 MapTest2.java):

/*
练习:
"sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。
希望打印结果:a(1)c(2).....

通过结果发现,每一个字母都有对应的次数。
说明字母和次数之间都有映射关系。

当发现有映射关系时,可以选择Map集合。
因为Map集合中存放的就是映射关系。
*/

/*
思路:
第一次用s字母作为键去找集合,那么集合没有s这个键,
所以也没有对应的次数,返回null。
如果为null,就将s字母和1存入集合。
如果指定的键已经存在,说明有对应的次数。
就将对应的次数取出,并自增后重新存入集合。
*/

/*
步骤:
1,将字符串转成字符数组,因为要对每一个字母进行操作。
2,定义一个Map集合,因为打印结果的字母有顺序,所以使用TreeMap集合。
3,遍历字符数组。
4,将Map集合中的数据变成指定的字符串返回。
*/
import java.util.*;
class MapTest2 
{
	public static void main(String[] args) 
	{
		String s = "sdfgzxcvasdfxcvdf";
		System.out.println(method(s));
	}
	public static String method(String s)
	{
		char[] chs = s.toCharArray();
		TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
		for(int x=0;x<chs.length;x++)
		{
			if(!(chs[x]>='a'&&chs[x]<='z'||chs[x]>='A'&&chs[x]<='Z'))  
				continue;
			Integer value = tm.get(chs[x]);
			if(value==null)
				tm.put(chs[x],1);
			else
			{
				value = value + 1;
				tm.put(chs[x],value);
			}
		}
		StringBuilder sb = new StringBuilder();
		Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
		Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();
		while(it.hasNext())
		{
			Map.Entry<Character,Integer> me = it.next();
			Character key = me.getKey();
			Integer value = me.getValue();
			sb.append(key+"("+value+")");
		}
		return sb.toString();
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值