(黑马程序员)学习笔记,Map集合

Map集合的简单应用

package map;

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

/**
 * Map集合
 * @author asus
 * Map
 * 	|--HashTable:哈希表数据结构,键和值都不能存入null,线程同步
 * 	|--HashMap:哈希表数据结构,键和值允许使用null,线程不同步
 * 	|--TreeMap:二叉树结构,键以先序遍历排序,线程不同步
 * 
 * 什么时候使用Map集合呢 
 * 当数据之间存在樱色关系的时候,就要想到使用map集合
 * 
 */
public class MapDemo {

	public static void main(String[] args) {
		Map<String,String> map = new HashMap<String,String>();
		
		//1.添加
		map.put("01", "javaI");
		map.put("02", "javaII");
		map.put("03", "javaIII");
		//判断包含
		System.out.println("containsKey:"+map.containsKey("02"));
		//获取指定的值
		System.out.println("get:"+map.get("02"));
		//获取所有的值
		Collection<String> coll = map.values();
		System.out.println("values = "+coll);
		//获取所有的键,然后再获取对应的值
		Collection<String> coll2 = map.keySet();
		Iterator<String> it2 = coll2.iterator();
		while(it2.hasNext()){
			String key = it2.next();
			String value = map.get(key);
			System.out.println("Collection: "+key+" = "+value);
		}
//		System.out.println("keys = "+coll2);
		//获取所有键值关系,然后获取所有键值
		Set<Map.Entry<String,String>> mapEntry = map.entrySet();
		Iterator<Map.Entry<String,String>> it = mapEntry.iterator();
		while(it.hasNext()){
			Map.Entry<String, String> entry = it.next();
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println("Entry: "+key+" = "+value);
		}
		//删除
		System.out.println("remove:"+map.remove("02"));
		
		
		System.out.println(map);
	}
}

接下来通过一个示例看看HashMap和TreeMap的区别

package map;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;

public class HashMapDemo {

	/*
	 * 通过比较可以看出,HashMap是无序的,而TreeMap的排序也是可以指定的
	 */
	public static void main(String[] args) {
		//新建一个HashMap
		HashMap<Student,String> hm = new HashMap<Student,String>();
		//向HashMap中添加几个值
		hm.put(new Student("test1",23), "beijing");
		hm.put(new Student("test2",26), "shanghai");
		hm.put(new Student("test3",22), "wuhan");
		hm.put(new Student("test4",23), "guangzhou");
		//使用迭代器取出HashMap中的键值对,并打印出来
		Iterator<Entry<Student,String>> it = hm.entrySet().iterator();
		while(it.hasNext()){
			Entry<Student,String> entry = it.next();
			System.out.println(entry.getKey().toString()+"..."+entry.getValue());
		}
		
		System.out.println("-- -- -- --");
		//新建一个TreeMap,并传入自定义比较器
		TreeMap<Student,String> tm = new TreeMap<Student,String>(new StudentComparator());
		//向TreeMap中添加几个值
		tm.put(new Student("test1",23), "beijing");
		tm.put(new Student("test2",26), "shanghai");
		tm.put(new Student("test3",22), "wuhan");
		tm.put(new Student("test4",23), "guangzhou");
		//使用迭代器取出TreeMap中的键值对,并打印出来
		Iterator<Entry<Student,String>> it2 = tm.entrySet().iterator();
		while(it2.hasNext()){
			Entry<Student,String> entry = it2.next();
			System.out.println(entry.getKey().toString()+"..."+entry.getValue());
		}
		
	}
}
//Student类
class Student implements Comparable<Student>{
	private String name;
	private int age;
	Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
	//重写HashCode()
	public int hashCode(){
		return name.hashCode()+age*13;
	}
	//重写toString()
	public String toString(){
		return name+"..."+age;
	}
	//重写equals()
	public boolean equals(Object o){
		if(!(o instanceof Student)){
			throw new RuntimeException("类型不匹配");
		}
		Student s = (Student)o;
		return this.name.equals(s.name) && this.age==s.age;
	}
	//重写compareTo(),可以让TreeMap根据自己的需要进行排序
	public int compareTo(Student o) {
		int result = this.name.compareTo(o.getName());
		if(result==0){
			return new Integer(this.age).compareTo(o.getAge());
		}
		 return result;
	}
}
//自定义比较类,用于TreeMap排序,实现Comparator接口,并作为创建TreeMap的参数
class StudentComparator implements Comparator<Student>{
	public int compare(Student s1,Student s2){
		int result = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
		if(result==0){
			return s1.getName().compareTo(s2.getName());
		}
		 return result;
	}
}

下面做一个小练习

package map;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

/*
 * 练习1:“abcdabdeacef”获取该字段的字符出现次数,并打印。希望打印结果是:a(3)b(2)c(2)...
 * 
 */
public class TreeMapDemo {

	private static String str;
	
	//从键盘录入字符串并保存起来
	private static void setStr(){
		Scanner scan = new Scanner(System.in);
		str = scan.nextLine();
	} 
	//计数
	public TreeMap<Character,Integer> countChar(){
		//调用输入方法,获得字符串
		TreeMapDemo.setStr();
		//将字符串转换成字符数组
		char[] ch = TreeMapDemo.str.toCharArray();
		//因为字符出现的次数和字符本身是一种对应关系,而且发现在输出时是abc...的顺序,这就想到了使用TreeMap进行存储和排序
		//新建一个TreeMap,并将字符数组中的元素添加到Map中
		TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
		for(int i=0;i<ch.length;i++){
			
			if(tm.containsKey(ch[i])){
				int value = tm.get(ch[i]);
				tm.put(ch[i], value+1);
			}else{
				tm.put(ch[i], 1);
			}
		}
		return tm;
	}
	//打印TreeMap
	public void printTreeMap(TreeMap<Character,Integer> tm){
		Set<Entry<Character,Integer>> set = tm.entrySet(); 
		//使用迭代器进行打印
		Iterator<Entry<Character,Integer>> it = set.iterator();
		while(it.hasNext()){
			Entry<Character,Integer> entry = it.next();
			System.out.print(entry.getKey()+"("+entry.getValue()+")");
		}
		
	}
	
	public static void main(String[] args) {
		TreeMapDemo tmd = new TreeMapDemo();
		TreeMap<Character,Integer> tm = tmd.countChar();
		tmd.printTreeMap(tm);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值