java之16天 Map集合 (二)

Map练习


/**练习
 * "sdfzesacwedcvde"获取该字符串中字母出现的次数
 * 希望打印结果为a(1)c(2)....
 * 
 * 通过 结果发现,没一个字母都有对应的次数.
 * 说明字母和次数之间都有映射关系
 * 
 * 注意了.当发现有映射关系时,可以选择map集合.
 * 因为Map集合中存放的就是映射关系.
 * 
 * 什么时候使用Map集合呢?
 * 但数据之间存在这种关系时,就要先想map集合.
 *
 * 思路:
 *  1.将字符串转换成字符数组,应要对每个字母进行操作
 *  2.定义Map容器,因为答应结果的字母有顺序,所以使用TreeMap集合
 *  3.遍历字符数组
 *     将每个字母作为键去查Map集合,如果返回null,将该祖母和1存入到map集合中
 *     如果返回不是null,说明该字母在map集合中不存在并有对应次数
 *     那么获取该此数据进行自增,然后键该字母和自增后的次数存入到map集合中,覆盖调用原来键所对应的值
 *   
 *  4.将map集合中的数据变成指定格式的字符串返回
 *   
 */
public class MapDemo4 {

	public static void main(String[] args) {
		
		System.out.println(charCount("aabx,.op.po./[fcdadsfdefa"));
	}
	
	public static String charCount(String str){
		char[] chs=str.toCharArray();
		TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
		Integer value=0;
		for(int x=0;x<chs.length;x++){
			//不统计非法字符
			if(!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='X'))
				continue;
			
			value=tm.get(chs[x]);
			if(value==null)
				value=0;
			value++;
			tm.put(chs[x], value);
			value=0;
		}
		Set<Map.Entry<Character, Integer>> sme=tm.entrySet();
		StringBuffer sb=new StringBuffer();
		Iterator<Map.Entry<Character,Integer>> it=sme.iterator();
		while(it.hasNext()){
			Map.Entry<Character, Integer> me=it.next();
			sb.append(me.getKey()+"("+me.getValue()+")");
		}
		
		return sb.toString();
	}

}


Map扩展



/**
 * Map扩展
 * map集合被使用,是因为具备映射关系
 * 
 * "预热帮" "01" "张三"
 * "预热帮" "02" "李四"
 * 
 * "就业办" "01" "王武"
 * "就业办" "02" "招六"
 * 
 */
class Students{
	private String id;
	private String name;
	Students(String id,String name){
		this.id=id;
		this.name=name;
	}
	public String getId(){
		return id;
	}
	
	public String getName(){
		return name;
	}
}

public class MapDemo5 {

	
	public static void main(String[] args) {
		method();
		System.out.println("================");
		method1();
	}

	public static void method1(){
		Map<String,List<Students>> school=new HashMap<String,List<Students>>();
		List<Students> yureban=new ArrayList<Students>();
		yureban.add(new Students("01","张三"));
		yureban.add(new Students("02","李四"));

		List<Students> jiuyeban=new ArrayList<Students>();
		yureban.add(new Students("01","王武"));
		yureban.add(new Students("02","招六"));

		school.put("yureban", yureban);
		school.put("jiuyeban", jiuyeban);
		
		
		Set<String> keySet=school.keySet();
		Iterator<String> it=keySet.iterator();
		while(it.hasNext()){
			String key=it.next();
			List<Students> list=school.get(key);
			getInfo(list);
		}
	}
	
	public static void getInfo(List<Students> list){
		
		Iterator<Students> it=list.iterator();
		while(it.hasNext()){
			Students str=it.next();
			System.out.println(str.getId()+":"+str.getName());
		}
		
	}
	public static void method(){
		Map<String,HashMap<String,String>> school=new HashMap<String,HashMap<String,String>>();  
		
		HashMap<String,String> yureban=new HashMap<String,String>();
		HashMap<String,String> jiuyeban=new HashMap<String,String>();
		yureban.put("01", "张三");
		yureban.put("02", "李四");
		
		jiuyeban.put("01", "王武");
		jiuyeban.put("02", "招六");
		
		school.put("yureban", yureban);
		school.put("jiuyeban", jiuyeban);
		
		getStuInfo(yureban);
		getStuInfo(jiuyeban);
		
		Set<String> keySet=school.keySet();
		
		Iterator<String> it=keySet.iterator();
		while(it.hasNext()){
			String key=it.next();
			System.out.println(key+":");
			HashMap<String,String> hm=school.get(key);
			getStuInfo(hm);
		}
		
		
	}
	
	public static void getStuInfo(HashMap<String,String> room){
		Iterator<String> it=room.keySet().iterator();
		while(it.hasNext()){
			String key=it.next();
			String value=room.get(key);
			System.out.println(key+":"+value);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值