Map:

(1)将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。

(2)Map和Collection的区别?

A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对

B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍

Map集合的数据结构值针对键有效,跟值无关

Collection集合的数据结构是针对元素有效

(3)Map接口功能概述

A:添加功能

V put(K key,V value):添加元素。

如果键是第一次存储,就直接存储元素,返回null

如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

B:删除功能

void clear():移除所有的键值对元素

V remove(Object key):根据键删除键值对元素,并把值返回

C:判断功能

boolean containsKey(Object key):判断集合是否包含指定的键

boolean containsValue(Object value):判断集合是否包含指定的值

boolean isEmpty():判断集合是否为空

D:获取功能

V get(Object key):根据键获取值

Set<K> keySet():获取集合中所有键的集合

Collection<V> values():获取集合中所有值的集合

Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合

E:长度功能

int size():返回集合中的键值对的对数

(4)Map集合的遍历

A:键找值

a:获取所有键的集合

b:遍历键的集合,得到每一个键

c:根据键到集合中去找值

B:键值对对象找键和值

a:获取所有的键值对对象的集合

b:遍历键值对对象的集合,获取每一个键值对对象

c:根据键值对对象去获取键和值

代码体现:

        import java.util.HashMap;import java.util.Map;import java.util.Set;/** Map集合的遍历。* Map -- 夫妻对* 思路:*         A:把所有的丈夫给集中起来。*         B:遍历丈夫的集合,获取得到每一个丈夫。*         C:让丈夫去找自己的妻子。** 转换:*         A:获取所有的键*         B:遍历键的集合,获取得到每一个键*         C:根据键去找值*/// 创建集合对象Map<String, String> map = new HashMap<String, String>();// 创建元素并添加到集合map.put("杨过", "小龙女");map.put("郭靖", "黄蓉");map.put("杨康", "穆念慈");map.put("陈玄风", "梅超风");// 遍历// 获取所有的键Set<String> set = map.keySet();// 遍历键的集合,获取得到每一个键for (String key : set) {// 根据键去找值String value = map.get(key);System.out.println(key + "---" + value);}}}
/** Map集合的遍历。* Map -- 夫妻对** 思路:*         A:获取所有结婚证的集合*         B:遍历结婚证的集合,得到每一个结婚证*         C:根据结婚证获取丈夫和妻子** 转换:*         A:获取所有键值对对象的集合*         B:遍历键值对对象的集合,得到每一个键值对对象*         C:根据键值对对象获取键和值**/// 创建集合对象Map<String, String> map = new HashMap<String, String>();// 创建元素并添加到集合map.put("杨过", "小龙女");map.put("郭靖", "黄蓉");map.put("杨康", "穆念慈");map.put("陈玄风", "梅超风");// 获取所有键值对对象的集合Set<Map.Entry<String, String>> set = map.entrySet();// 遍历键值对对象的集合,得到每一个键值对对象for (Map.Entry<String, String> me : set) {// 根据键值对对象获取键和值String key = me.getKey();String value = me.getValue();System.out.println(key + "---" + value);}

(5)HashMap集合的练习

HashMap:是基于哈希表的Map接口实现。 哈希表的作用是用来保证键的唯一性的

A:HashMap<String,String>

B:HashMap<Integer,String>


/** HashMap<Integer,String>* 键:Integer* 值:String*/// 创建集合对象HashMap<Integer, String> hm = new HashMap<Integer, String>();// 创建元素并添加元素hm.put(27, "林青霞");hm.put(30, "风清扬");hm.put(28, "刘意");hm.put(29, "林青霞");// 下面的写法是八进制,但是不能出现8以上的单个数据// hm.put(003, "hello");// hm.put(006, "hello");// hm.put(007, "hello");// hm.put(008, "hello");// 遍历Set<Integer> set = hm.keySet();for (Integer key : set) {String value = hm.get(key);System.out.println(key + "---" + value);}// 下面这种方式仅仅是集合的元素的字符串表示// System.out.println("hm:" + hm);

C:HashMap<String,Student>

           /** HashMap<String,Student>* 键:String    学号* 值:Student 学生对象*/// 创建集合对象HashMap<String, Student> hm = new HashMap<String, Student>();// 创建学生对象Student s1 = new Student("周星驰", 58);Student s2 = new Student("刘德华", 55);Student s3 = new Student("梁朝伟", 54);Student s4 = new Student("刘嘉玲", 50);// 添加元素hm.put("9527", s1);hm.put("9522", s2);hm.put("9524", s3);hm.put("9529", s4);// 遍历Set<String> set = hm.keySet();for (String key : set) {// 注意了:这次值不是字符串了// String value = hm.get(key);Student value = hm.get(key);System.out.println(key + "---" + value.getName() + "---"+ value.getAge());}

D:HashMap<Student,String>

E:LinkedHashMap<String, String>

/** LinkedHashMap:是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。* 由哈希表保证键的唯一性* 由链表保证键盘的有序(存储和取出的顺序一致)*/// 创建集合对象LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();// 创建并添加元素hm.put("2345", "hello");hm.put("1234", "world");hm.put("3456", "java");hm.put("1234", "javaee");hm.put("3456", "android");// 遍历Set<String> set = hm.keySet();for (String key : set) {String value = hm.get(key);System.out.println(key + "---" + value);}

(6)TreeMap集合的练习

TreeMap:是基于红黑树的Map接口的实现。

A:TreeMap<String,String>

   // 创建集合对象TreeMap<String, String> tm = new TreeMap<String, String>();// 创建元素并添加元素tm.put("hello", "你好");tm.put("world", "世界");tm.put("java", "爪哇");tm.put("world", "世界2");tm.put("javaee", "爪哇EE");// 遍历集合Set<String> set = tm.keySet();for (String key : set) {String value = tm.get(key);System.out.println(key + "---" + value);}

B:TreeMap<Student,String>

// 创建集合对象TreeMap<Student, String> tm = new TreeMap<Student, String>(new Comparator<Student>() {@Overridepublic 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);}

(7)案例

A:统计一个字符串中每个字符出现的次数


/** 需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)** 分析:*         A:定义一个字符串(可以改进为键盘录入)*         B:定义一个TreeMap集合*             键:Character*             值:Integer*         C:把字符串转换为字符数组*         D:遍历字符数组,得到每一个字符*         E:拿刚才得到的字符作为键到集合中去找值,看返回值*             是null:说明该键不存在,就把该字符作为键,1作为值存储*             不是null:说明该键存在,就把值加1,然后重写存储该键和值*         F:定义字符串缓冲区变量*         G:遍历集合,得到键和值,进行按照要求拼接*         H:把字符串缓冲区转换为字符串输出** 录入:linqingxia* 结果:result:a(1)g(1)i(3)l(1)n(2)q(1)x(1)*/public class TreeMapDemo {public static void main(String[] args) {// 定义一个字符串(可以改进为键盘录入)Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串:");String line = sc.nextLine();// 定义一个TreeMap集合TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();//把字符串转换为字符数组char[] chs = line.toCharArray();//遍历字符数组,得到每一个字符for(char ch : chs){//拿刚才得到的字符作为键到集合中去找值,看返回值Integer i =  tm.get(ch);//是null:说明该键不存在,就把该字符作为键,1作为值存储if(i == null){tm.put(ch, 1);}else {//不是null:说明该键存在,就把值加1,然后重写存储该键和值i++;tm.put(ch,i);}}//定义字符串缓冲区变量StringBuilder sb=  new StringBuilder();//遍历集合,得到键和值,进行按照要求拼接Set<Character> set = tm.keySet();for(Character key : set){Integer value = tm.get(key);sb.append(key).append("(").append(value).append(")");}//把字符串缓冲区转换为字符串输出String result = sb.toString();System.out.println("result:"+result);}}

B:集合的嵌套遍历

a:HashMap嵌套HashMap


/** HashMap嵌套HashMap**         jc    基础班*                 陈玉楼        20*                 高跃         22*         jy    就业班*                 李杰          21*                 曹石磊        23** 先存储元素,然后遍历元素*/public class HashMapDemo2 {public static void main(String[] args) {// 创建集合对象HashMap<String, HashMap<String, Integer>> czbkMap = new HashMap<String, HashMap<String, Integer>>();// 创建基础班集合对象HashMap<String, Integer> jcMap = new HashMap<String, Integer>();// 添加元素jcMap.put("陈玉楼", 20);jcMap.put("高跃", 22);// 把基础班添加到大集合czbkMap.put("jc", jcMap);// 创建就业班集合对象HashMap<String, Integer> jyMap = new HashMap<String, Integer>();// 添加元素jyMap.put("李杰", 21);jyMap.put("曹石磊", 23);// 把基础班添加到大集合czbkMap.put("jy", jyMap);//遍历集合Set<String> czbkMapSet = czbkMap.keySet();for(String czbkMapKey : czbkMapSet){System.out.println(czbkMapKey);HashMap<String, Integer> czbkMapValue = czbkMap.get(czbkMapKey);Set<String> czbkMapValueSet = czbkMapValue.keySet();for(String czbkMapValueKey : czbkMapValueSet){Integer czbkMapValueValue = czbkMapValue.get(czbkMapValueKey);System.out.println("\t"+czbkMapValueKey+"---"+czbkMapValueValue);}}}}

b:HashMap嵌套ArrayList


/**需求:*假设HashMap集合的元素是ArrayList。有3个。*每一个ArrayList集合的值是字符串。*元素我已经完成,请遍历。*结果:*         三国演义*             吕布*             周瑜*         笑傲江湖*             令狐冲*             林平之*         神雕侠侣*             郭靖*             杨过 */public class HashMapIncludeArrayListDemo {public static void main(String[] args) {// 创建集合对象HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();// 创建元素集合1ArrayList<String> array1 = new ArrayList<String>();array1.add("吕布");array1.add("周瑜");hm.put("三国演义", array1);// 创建元素集合2ArrayList<String> array2 = new ArrayList<String>();array2.add("令狐冲");array2.add("林平之");hm.put("笑傲江湖", array2);// 创建元素集合3ArrayList<String> array3 = new ArrayList<String>();array3.add("郭靖");array3.add("杨过");hm.put("神雕侠侣", array3);//遍历集合Set<String> set = hm.keySet();for(String key : set){System.out.println(key);ArrayList<String> value = hm.get(key);for(String s : value){System.out.println("\t"+s);}}}}

c:ArrayList嵌套HashMap


/*ArrayList集合嵌套HashMap集合并遍历。需求:假设ArrayList集合的元素是HashMap。有3个。每一个HashMap集合的键和值都是字符串。元素我已经完成,请遍历。结果:周瑜---小乔吕布---貂蝉郭靖---黄蓉杨过---小龙女令狐冲---任盈盈林平之---岳灵珊*/public class ArrayListIncludeHashMapDemo {public static void main(String[] args) {// 创建集合对象ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();// 创建元素1HashMap<String, String> hm1 = new HashMap<String, String>();hm1.put("周瑜", "小乔");hm1.put("吕布", "貂蝉");// 把元素添加到array里面array.add(hm1);// 创建元素1HashMap<String, String> hm2 = new HashMap<String, String>();hm2.put("郭靖", "黄蓉");hm2.put("杨过", "小龙女");// 把元素添加到array里面array.add(hm2);// 创建元素1HashMap<String, String> hm3 = new HashMap<String, String>();hm3.put("令狐冲", "任盈盈");hm3.put("林平之", "岳灵珊");// 把元素添加到array里面array.add(hm3);// 遍历for (HashMap<String, String> hm : array) {Set<String> set = hm.keySet();for (String key : set) {String value = hm.get(key);System.out.println(key + "---" + value);}}}}

d:多层嵌套


/** 为了更符合要求:*         这次的数据就看成是学生对象。**         bj    北京校区*             jc    基础班*                     林青霞        27*                     风清扬        30*             jy    就业班   *                     赵雅芝        28*                     武鑫        29*         xa    西安校区*             jc    基础班*                     范冰冰        27*                     刘意        30*             jy    就业班   *                     李冰冰        28*                     张志豪        29*/public class HashMapDemo {public static void main(String[] args) {// 创建大集合HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();// 北京校区数据HashMap<String, ArrayList<Student>> bjCzbkMap = new HashMap<String, ArrayList<Student>>();ArrayList<Student> array1 = new ArrayList<Student>();Student s1 = new Student("林青霞", 27);Student s2 = new Student("风清扬", 30);array1.add(s1);array1.add(s2);ArrayList<Student> array2 = new ArrayList<Student>();Student s3 = new Student("赵雅芝", 28);Student s4 = new Student("武鑫", 29);array2.add(s3);array2.add(s4);bjCzbkMap.put("基础班", array1);bjCzbkMap.put("就业班", array2);czbkMap.put("北京校区", bjCzbkMap);// 上海校区数据..// 广州校区数据...// 西安校区数据HashMap<String, ArrayList<Student>> xaCzbkMap = new HashMap<String, ArrayList<Student>>();ArrayList<Student> array3 = new ArrayList<Student>();Student s5 = new Student("范冰冰", 27);Student s6 = new Student("刘意", 30);array3.add(s5);array3.add(s6);ArrayList<Student> array4 = new ArrayList<Student>();Student s7 = new Student("李冰冰", 28);Student s8 = new Student("张志豪", 29);array4.add(s7);array4.add(s8);xaCzbkMap.put("基础班", array3);xaCzbkMap.put("就业班", array4);czbkMap.put("西安校区", xaCzbkMap);// 遍历集合Set<String> czbkMapSet = czbkMap.keySet();for (String czbkMapKey : czbkMapSet) {System.out.println(czbkMapKey);HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap.get(czbkMapKey);Set<String> czbkMapValueSet = czbkMapValue.keySet();for (String czbkMapValueKey : czbkMapValueSet) {System.out.println("\t" + czbkMapValueKey);ArrayList<Student> czbkMapValueValue = czbkMapValue.get(czbkMapValueKey);for (Student s : czbkMapValueValue) {System.out.println("\t\t" + s.getName() + "---"+ s.getAge());}}}}}

Map集合(HashMap,TreeMap)学习总结以及经典案例相关推荐

  1. Map集合HashMap TreeMap的输出方法

    Map集合HashMap TreeMap的输出方法     [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/51934301 ...

  2. java hashmap api_JAVA基础学习-集合三-Map、HashMap,TreeMap与常用API

    一.Map简述 1.1.简述 public interface Map 类型参数:K - 此映射所维护的键的类型 keyV - 映射值的类型 value该集合提供键--值的映射.key不能重复,一对对 ...

  3. Map集合HashMap,TreeMap

    Map集合没有继承Collection接口,其提供的是key到value的映射.Map集合中不能包含相同的key.每个key只能映射一个value.key还决定存储对象在映射中的存储位置,但不是有ke ...

  4. Set集合[HashSet,TreeSet,LinkedHashSet],Map集合[HashMap,HashTable,TreeMap]

    ------------ Set ------------------- 有序: 根据添加元素顺序判定, 如果输出的结果和添加元素顺序是一样 无序: 根据添加元素顺序判定,如果输出的结果和添加元素的顺 ...

  5. html 空行_一篇文章学习html「经典案例」

    html叫做超本文标记语言,注意它只是标记语言,不是编程语言. 编写规范: 由标记(html, div, p, h1等)组成 标记成对出现(...),也有例外,比如. 标记有层级关系 标记有内容文本或 ...

  6. html经典案例pdf,一篇文章学习html【经典案例】

    html叫做超本文标记语言,注意它只是标记语言,不是编程语言. 编写规范: 由标记(html, div, p, h1等)组成 标记成对出现(...),也有例外,比如. 标记有层级关系 标记有内容文本或 ...

  7. JavaScript超全学习笔记+Web经典案例,赶快收藏

    青春啊,永远是美好的,可是真正的青春,只属于这些永远力争上游的人,永远忘我劳动的人,永远谦虚的人! 最近收到不少的优秀学生投稿,他们有投稿手写笔记.有手写博客.也还有视频.小编也从中挑出几篇较为优秀的 ...

  8. JavaSE学习总结(十四)Map集合/Map和Collection的区别/HashMap/LinkedHashMap/TreeMap/集合间的嵌套/Hashtable/Collections工具类

    一.Map集合 我们知道,一个学号就能对应一个学生,并且每个学生的学号都不同,学号就像一个键,对应的学生就是该键对应的值.日常生活中经常能见到这种类似学号对应学生的例子.Java 为了我们更加方便地去 ...

  9. 集合类(collection、List、Set、Map、HashMap、TreeMap、、Properties、泛型类、可变长参数、增强for循环、Collections、数组与链表)

    集合类 0.集合类概述 1.Collection 概念 (1)Collection接口的API (2)Iterator接口(collection集合的遍历) 案例:collection集合存储学生对象 ...

最新文章

  1. HDU 1003 Maxsum
  2. 数据结构与算法专题——第二题 优先队列
  3. 鸿蒙 电视 安卓,华为鸿蒙2.0来了!打通手机、电视、PC全平台,Mate 40 整装齐发...
  4. python pandas 排序_Pandas的排序和排名(Series, DataFrame) + groupby
  5. android 异步图片加载 开源,异步加载图片BitmapFun分析
  6. react学习系列3 使用koa-router模拟后台接口
  7. HCIE RS 全套笔记整理
  8. [51nod1084]矩阵取数问题 V2
  9. Jenkins+CCNET的另类部署图
  10. 汽车车牌正则表达式验证,含新能源车牌(适用全国)
  11. 一些常用的Maven命令的作用
  12. Windows 技术篇-修改电脑时间格式显示为12小时、24小时时间制式方法
  13. Vue通过nginx转发后dist文件页面样式丢失
  14. 【讲座】清华刘知远:自然语言理解难在哪儿?
  15. 四年嵌入式技术沉淀,处女作《STM32CubeMX系列实战教程》诞生
  16. PPT模板下载地址汇总(免费)
  17. ZJCTF-Triple Language Write up
  18. 【微信小程序】全局变量的定义与使用
  19. maven项目安装本地包实战演示
  20. 源代码托管网站BitBucket

热门文章

  1. 阿里云数据中台加速场景化 Quick系列产品新增猛将
  2. CAD二次开发--系统找不到指定的文件/应用程序不支持实时(JIT)调试解决办法【即:C#调用外部exe文件在CAD内部执行失败处理办法】
  3. 四参数转换 matlab,四参数转化模型.pdf
  4. java实现浏览器下载文件,并解决兼容各浏览器的乱码与后缀问题
  5. 22-移动端布局方式-rem
  6. 常见编程语言OEP入口整理
  7. 几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色
  8. Error记录--make: ./libtool:命令未找到
  9. 推荐算法之召回模型:DSSM、YoutubeDNN
  10. 小菜鸡近期的一些胡思乱想