01)Map(概述)
- /*
- * Map<K, V>:该集合存储键值对。一对一对的往里存,而且要保证键值的唯一性。
- * 1:添加:
- * put(K key, V value):
- * putAll(Map<? extends K,? extends V> m):
- * 2:删除:
- * clear():
- * remove(Object key):
- * 3:判断:
- * containsKey(Object key):
- * containsValue(Object value):
- * isEmpty():
- * 4:获取:
- * get(Object key):
- * size():
- * values():
- * **entrySet():
- * **keySet():
- *
- * |---Map
- * |---Hashtable
- * |---HashMatp
- * |---TreeMap
- */
02)Map子类对象的特点
- /*
- * |---Map
- * |---Hashtable:底层是哈希表数据结构,不可以存入Null值和Null键。该集合是线程同步的。jdk1.0:效率低
- * 为了成功地在哈希表中存储和获取对象,用作键的对象必须实现 hashCode 方法和 equals 方法。
- * |---HashMap:底层是哈希表数据结构,并允许使用Null值和Null键。该集合是线程不同步的。jdk1.2:效率高
- * |---TreeMap:底层是二叉树数据结构,该集合是线程不同步的。可以用于Map集合中键进行排序。
- *
- * Map和Set很像。其实Set底层使用的就是Map集合。
- *
- */
03)Map——共性方法
- /*
- * Map——共性方法。
- */
- public class Map03 {
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- method_1();
- method_2();
- }
- public static void method_1(){
- Map<String, String> map = new HashMap<String, String>();
- //添加元素。
- map.put("01", "汤姆一号");
- map.put("02", "汤姆二号");
- map.put("03", "汤姆三号");
- map.put("05", "汤姆四号");
- map.put("06", "汤姆五号");
- sop("containsKey = " + map.containsKey("02"));
- sop("(04)remove = " + map.remove("04"));
- sop("(05)get = " + map.get("05"));
- sop("(04)get = " + map.get("04"));
- sop(map.put("07", "汤姆七号"));
- sop("Map " + map);
- System.out.println("--------------------------------------------------------------");
- }
- public static void method_2(){
- Map<String, String> map = new HashMap<String, String>();
- //添加元素。
- map.put("01", "汤姆一号");
- map.put("02", "汤姆二号");
- map.put("03", "汤姆三号");
- map.put("05", "汤姆四号");
- map.put("06", "汤姆五号");
- //获取集合中的所以元素。
- Collection<String> coll = map.values();
- sop("coll " + coll);
- sop("Map " + map);
- //添加元素,如果出现添加相同的键,那么后添加的值会覆盖原有的键所对应的值。
- //put方法会返回覆盖的值。
- sop("(06)Put = " + map.put("06", "错误汤姆编号"));//后加值将替代原值。
- sop("(06)(null)Put = " + map.put("06", null));
- sop("Map " + map);
- System.out.println("--------------------------------------------------------------");
- }
- }

04)Map——keySet
- /*
- * Map集合的两种取出方式:
- * Map集合的取出原理:将map集合转成set集合。再通过迭代器取出。
- * 1:Set<K> keySet:将Map中所有的键存入到Set集合中,因为Set具备迭代器,所以可以通过迭代方式取出所有的键。
- * 再根据get方法,获取每一个键的值。
- * 2:Set<Map.Entry<K, V>> entrySet:将map集合中的映射关系存入到了set集合中。
- * 而这个关系的数据类型就是:Map.Entry。
- */
- public class MapkeySetDemo {
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- ks_method();
- }
- public static void ks_method(){
- Map<String, String> map = new HashMap<String, String>();
- //添加元素。
- map.put("01", "汤姆一号");
- map.put("02", "汤姆二号");
- map.put("03", "汤姆三号");
- map.put("05", "汤姆五号");
- map.put("06", "汤姆六号");
- //先获取Map集合中所有键的Set集合。keySet()方法。
- Set<String> keySet = map.keySet();
- //通过set集合,获取其迭代器。
- for (Iterator<String> it = keySet.iterator(); it.hasNext();){
- String key = it.next();
- //键被获取后,可以通过Map集合的get(key)方法获取其对应的值。
- sop("(键)key = " + key);
- String value = map.get(key);
- sop("(值) = " + value);
- }
- }
- }
运行结果如下图所示:

05)Map——entrySet
- /*
- * Map集合的两种取出方式:
- * 1:Set<K> keySet:
- * 2:Set<Map.Entry<K, V>> entrySet:将map集合中的映射关系存入到了set集合中。
- * 而这个关系的数据类型就是:Map.Entry。
- * K getKey():返回与此项对应的键。
- * V getValue():返回与此项对应的值。
- */
- public class MapentrySetDemo {
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- ets_method();
- }
- public static void ets_method(){
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(01, "汤姆一号");
- map.put(02, "汤姆二号");
- map.put(03, "汤姆三号");
- map.put(05, "汤姆五号");
- map.put(06, "汤姆六号");
- //将map集合中的映射关系取出。存入到Set集合中。
- Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
- for (Iterator<Map.Entry<Integer, String>> it = entrySet.iterator(); it.hasNext();){
- Map.Entry<Integer, String> me = it.next();
- Integer key = me.getKey();
- String value = me.getValue();
- sop(key + " = " + value);
- }
- }
- }
- //(Map.Entry)的真相示例。
- //interface Map{
- // public static interface Entry{
- // public abstract Object getKey();
- // public abstract Object getValue();
- // }
- //}
- //class HashMap implements Map{
- // class XX implements Entry{
- // public abstract Object getKey(){
- //
- // }
- // public abstract Object getValue(){
- //
- // }
- // }
- //}
运行结果如下图所示:

06)Map——HashSet(练习)
- /*
- * 每一个学生都有对应的归属地。
- * (学生)Student,(地址)String。
- * 学生属性:(姓名),(年龄)。
- * 注意:姓名和年龄相同的视为同一个学生。
- * 保证学生的唯一性。
- *
- * 1:描述学生。
- * 2:定义map容器。将学生作为键,地址作为值。存入。
- * 3:获取map集合中的元素。
- */
- public class HashMapTest {//使用HashMap
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- method();
- }
- public static void method(){
- HashMap<Student, String> hm = new HashMap<Student, String>();
- hm.put(new Student("汤姆一号", 21), "金星");
- hm.put(new Student("汤姆二号", 22), "木星");
- hm.put(new Student("汤姆三号", 23), "水星");
- hm.put(new Student("汤姆四号", 24), "火星");
- hm.put(new Student("汤姆五号", 25), "土星");
- hm.put(new Student("汤姆二号", 22), "月球");
- hm.put(new Student("汤姆三号", 23), "土星");
- //第一种取出方式 keySet
- Set<Student> keySet = hm.keySet();
- for (Student it : keySet){//it等于it.next()。
- sop(it + " " + hm.get(it));
- }
- System.out.println("------------------------");
- //第二种取出方式 entrySet
- Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
- for (Map.Entry<Student, String> entry: entrySet){
- sop(entry.getKey() + " = " + entry.getValue());
- }
- }
- }
- class Student implements Comparable<Student>{
- private String name;
- private int age;
- Student(String name, int age){
- this.name = name;
- this.age = age;
- }
- public int compareTo(Student s){//覆盖compareTo方法。
- int num = new Integer(this.age).compareTo(new Integer(s.age));
- if (num == 0)
- return this.name.compareTo(s.name);
- return num;
- }
- public int hashCode(){
- return name.hashCode() + age * age;
- }
- 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 String getName(){
- return name;
- }
- // public void setName(String name){
- // this.name = name;
- // }
- public int getAge(){
- return age;
- }
- // public void setAge(int age){
- // this.age = age;
- // }
- public String toString(){
- return name + ":" + age;
- }
- }
运行结果如下图所示:

07)Map——TreeMap(练习)
- /*
- * 需求:对学生对象的年龄进行升序排序。
- */
- public class TreeMapTest {//使用TreeMap。
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- method();
- }
- public static void method(){
- TreeMap<Student_T_P, String> tm = new TreeMap<Student_T_P, String>(new MyCom());
- tm.put(new Student_T_P("汤姆七号", 27), "火星");
- tm.put(new Student_T_P("汤姆五号", 25), "土星");
- tm.put(new Student_T_P("汤姆二号", 22), "木星");//"木星"被"月球"覆盖。
- tm.put(new Student_T_P("汤姆三号", 23), "水星");//"水星"被"土星"覆盖。
- tm.put(new Student_T_P("汤姆四号", 24), "火星");
- tm.put(new Student_T_P("汤姆二号", 22), "月球");
- tm.put(new Student_T_P("汤姆三号", 22), "土星");
- tm.put(new Student_T_P("汤姆一号", 21), "金星");
- tm.put(new Student_T_P("汤姆一号", 26), "金星");
- //第一种取出方式 keySet
- Set<Student_T_P> keySet = tm.keySet();
- for (Student_T_P stu : keySet){//it等于it.next()。
- sop(stu + "(age)" + tm.get(stu));
- }
- }
- }
- class Student_T_P implements Comparable<Student_T_P>{
- private String name;
- private int age;
- Student_T_P(String name, int age){
- this.name = name;
- this.age = age;
- }
- public int compareTo(Student_T_P s){
- int num = new Integer(this.age).compareTo(new Integer(s.age));
- if (num == 0)
- return this.name.compareTo(s.name);
- return num;
- }
- public int hashCode(){
- return name.hashCode() + age * age;
- }
- public boolean equals(Object obj){
- if (!(obj instanceof Student_T_P))
- throw new ClassCastException("类型不匹配");
- Student_T_P s = (Student_T_P)obj;
- return this.name.equals(s.name) && this.age == s.age;
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public int getAge(){
- return age;
- }
- public void setAge(int age){
- this.age = age;
- }
- public String toString(){
- return name + " = " + age;
- }
- }
- class MyCom implements Comparator{
- public int compare(Object o1, Object o2){
- Student_T_P stu1 = (Student_T_P)o1;
- Student_T_P stu2 = (Student_T_P)o2;
- //按age排序。
- int num = new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge()));
- if (num == 0)
- return stu1.getName().compareTo(stu2.getName());
- return num;
- }
- }
运行结果如下图所示:

08)字母出现的次数(TreeSet)
- /*
- * "afdhsafklhasdlweoradlfkj"获取该字符串中每个字母出现的次数。
- * 预期打印结果格式为:a(?)b(?)...
- *
- * 思路:
- * 1:将字符串转换成字符数组。因为要对每一个字母进行操作。
- * 2:定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合。
- * 3:遍历字符数组。
- * 将每一个字母作为键去查map集合。
- * 如果返回null,将该字母和1存入到map集合中。
- * 如果返回不是null,说明该字母在map集合已经存在并有对应次数。
- * 那么就获取该次数并进行自增。,然后将该字母和自增后的次数存入到map集合中。覆盖调用原理键所对应的值。
- * 4:将map集合中的数据变成指定的字符串形式返回。
- */
- public class TreeMapCountTest {
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- sop(method("afdhsb3afafkl>c-kkfahasdsdg=l,weorafdgsd&l5fkj"));
- }
- public static String method(String s){
- //将字串符转换成字符数组。
- char[] ch = s.toCharArray();
- TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
- int count = 0;
- for (int i = 0; i < ch.length; i++){
- if(!(ch[i]>='a' && ch[i]<='z' || ch[i]>='A' && ch[i]<='Z'))//如果键不是在a~z之间。
- continue;//退出本次循环。
- Integer value = tm.get(ch[i]);//通过键获取值。
- if (value != null)//如果有值。
- count = value;
- count++;//将计数器加1。
- tm.put(ch[i], count);//将键与值一起存入。
- count = 0;//清零。
- }
- StringBuilder sb = new StringBuilder();
- Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();
- for(Map.Entry<Character, Integer> key : entrySet){
- sb.append(key.getKey() + "(" + key.getValue() + ")");//添加键与值。
- }
- return sb.toString();
- }
- }
