A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小K哥 中级黑马   /  2016-5-6 22:38  /  373 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一:Map的功能概述:
(一)Map集合的特点:
                将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
(二)注意:
                  Map集合的数据结构值针对键有效,跟值无关                        
                Collection集合的数据结构是针对元素有效
(三)Map集合的方法概述:
                  1:添加功能
                                  V put(K key,V value):添加元素。如果键是第一次存储,就直接存储元素,返回null
                                                                        如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
                  2:删除功能
                                  void clear():移除所有的键值对元素
                                  V remove(Object key):根据键删除键值对元素,并把值返回
                  3:判断功能
                                  boolean containsKey(Object key):判断集合是否包含指定的键
                                  boolean containsValue(Object value):判断集合是否包含指定的值
                                  boolean isEmpty():判断集合是否为空
                  4:获取功能
                                  Set<Map.Entry<K,V>> entrySet():返回此映射中包含的映射关系的 Set 视图
                                  V get(Object key):根据键获取值
                                  Set<K> keySet():获取集合中所有键的集合
                                  Collection<V> values():获取集合中所有值的集合
                  5:长度功能
                                  int size():返回集合中的键值对的对数
(四)Map集合的两种遍历方式:
                Map集合的遍历:
                                方式一:                       
                                                  A:获取所有的键
                                                        :Set<K> keySet():获取集合中所有键的集合
                                                  B:遍历键的集合,获取得到每一个键
                                                  C:根据键去找值
                         
                                方式二:                                                       
                                                  A:获取所有键值对对象的集合
                                                        :Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合
                                                  B:遍历键值对对象的集合,得到每一个键值对对象
                                                  C:根据键值对对象获取键和值       
(五)Map集合和Collection集合的区别?
                  Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的。可以把这个理解为:夫妻对
                  Collection集合存储元素是单独出现的,Collection的儿子Set是唯一的,List是可重复的。                                               
二:HashMap(Map的子类)的功能概述:

(一)是基于哈希表的Map接口实现。
                        哈希表的作用是用来保证键的唯一性的。
                       
(二)Hashtable和HashMap的区别?
                          Hashtable:线程安全,效率低。不允许null键和null值
                          HashMap:线程不安全,效率高。允许null键和null值
                  
(三)List,Set,Map等接口是否都继承子Map接口?
                          List,Set不是继承自Map接口,它们继承自Collection接口
                          Map接口本身就是一个顶层接口
                          
三:TreeMap(Map的子类)的功能概述:
(一)TreeMap:是基于红黑树的Map接口的实现。
                有两种排序方式:
                                A:自然排序(元素具备比较性)
                                        让元素所属的类实现自然排序接口 Comparable
                                B:比较器排序(集合具备比较性)
                                        让集合的构造方法接收一个比较器接口的子类对象 Comparator
                                       
四:LinkedHashMap(HashMap的子类)的功能概述:
(一)是Map接口的哈希表和链表实现,具有可预知的迭代顺序。
                        由哈希表保证键的唯一性
                        由链表保证键盘的有序(存储和取出的顺序一致)
五:例子部分:
(一)
                代码实现:(Student类省略,有name\age)       
                        public class TreeMapDemo2 {
                                                public static void main(String[] args) {
                                                        // 创建集合对象
                                                        TreeMap<Student, String> tm = new TreeMap<Student, String>(
                                                                        new Comparator<Student>() {
                                                                                @Override
                                                                                public int compare(Student s1, Student s2) {
                                                                                        // 主要条件
                                                                                        int num = s1.getAge() - s2.getAge();
                                                                                        // 次要条件
                                                                                        int num2 = num == 0 ? s1.getName().compareTo(
                                                                                                        s2.getName()) : num;
                                                                                        return num2;
                                                                                }
                                                                        });
                                                        // 创建学生对象
                                                        Student s1 = new Student("潘安", 30);
                                                        Student s2 = new Student("柳下惠", 35);
                                                        Student s3 = new Student("唐伯虎", 33);
                                                        Student s4 = new Student("燕青", 32);
                                                        Student s5 = new Student("唐伯虎", 33);
                                                        // 存储元素
                                                        tm.put(s1, "宋朝");
                                                        tm.put(s2, "元朝");
                                                        tm.put(s3, "明朝");
                                                        tm.put(s4, "清朝");
                                                        tm.put(s5, "汉朝");
                                                        // 遍历
                                                        Set<Student> set = tm.keySet();
                                                        for (Student key : set) {
                                                                String value = tm.get(key);
                                                                System.out.println(key.getName() + "---" + key.getAge() + "---"
                                                                                + value);
                                                        }
                                                }
                                        }
(二)键盘录入2个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
                  分析:
                   A:创建学生类
                   B:创建键盘录入
                   C:创建TreeSet集合,用集合存储学生对象
                   D:用Comparator类排序
                   E:遍历集合
                代码实现:
                        public class TreeSetDemo2 {
                                public static void main(String[] args) {
                                        //创建键盘录入
                                        Scanner reader=new Scanner(System.in);
                                        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
                                                @Override
                                                public int compare(Student s1, Student s2) {
                                                        int num=s2.getSum()-s1.getSum();
                                                        int num2=num==0?s1.getName().compareTo(s2.getName()):num;
                                                        int num3=num2==0?s1.getChinese()-s2.getChinese():num2;
                                                        int num4=num3==0?s1.getMath()-s2.getMath():num3;
                                                        int num5=num4==0?s1.getEnglish()-s2.getEnglish():num4;
                                                        return num5;
                                                }
                                        });
                                        for(int i=1;i<=2;i++){
                                                System.out.println("请输入第"+i+"个学生的信息");
                                                System.out.println("请输入姓名:");
                                                String name=reader.nextLine();
                                                System.out.println("请输入语文成绩:");
                                                Integer chinese=Integer.valueOf(reader.nextLine());
                                                System.out.println("请输入数学成绩:");
                                                Integer math=Integer.valueOf(reader.nextLine());
                                                System.out.println("请输入英语成绩:");
                                                Integer english=Integer.valueOf(reader.nextLine());
                                                Student s=new Student();
                                                s.setName(name);
                                                s.setChinese(chinese);
                                                s.setMath(math);
                                                s.setEnglish(english);
                                                //创建TreeSet集合,用集合存储学生对象
                                                ts.add(s);               
                                        }
                                        // 遍历集合
                                        System.out.println("姓名\t语文成绩\t数学成绩\t英语成绩");
                                                        for (Student s : ts) {
                                                                System.out.println(s.getName() + "\t" + s.getChinese() + "\t"
                                                                                + s.getMath() + "\t" + s.getEnglish());
                                                        }
                                }
                        }
(三)需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
                  分析:
                  A:创建键盘录入
                  B:用字符串接收键盘录入数据
                  C:把字符串转出字符数组
                  D:遍历字符数组
                  E:创建TreeMap集合
                  F:把遍历的的每一个字符和TreeMap的键比较.
                                                  相同:值加一
                                                  不相同:添加,并且值为一
                  G:遍历集合
        代码实现:
                public class TreeMapDemo3 {
                        public static void main(String[] args) {
                                //创建键盘录入
                                Scanner reader=new Scanner(System.in);
                                //用字符串接收键盘录入数据
                                String s=reader.nextLine();
                                //把字符串转出字符数组
                                char[] ch=s.toCharArray();
                                //创建TreeMap集合
                                TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
                                //遍历字符数组
                                for(char c:ch){
                                        Integer num=tm.get(c);
                                        if(num==null){
                                                tm.put(c, 1);//不相同:添加,并且值为一
                                        }else{
                                                num++;//相同:值加一
                                                tm.put(c, num);
                                        }
                                }
                                //遍历集合
                                Set<Character> set=tm.keySet();
                                StringBuffer sb=new StringBuffer();
                                for(Character key:set){
                                        sb.append(key).append("(").append(tm.get(key)).append(")");               
                                }
                                System.out.println(sb.toString());
                        }
                }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马