首页 > 其他 > 详细

12集合(3)-----Map

时间:2019-03-24 13:23:34      阅读:150      评论:0      收藏:0      [点我收藏+]

 一.总体分类

  • Collection(包括方法add,remove,contains,clear,size)
  1. List(接口)

      LinkedList

      ArrayList

      Vector---Stack

   2. Set

      HashSet

      TreeSet

  • Map
  1. Hashtable---properties
  2. HashMap
  3. TreeMap

二.Map接口:元素以键值对的方式存放(无序)

  HashMap:键不能重复,当添加重复的时候,会将原来的值覆盖掉

       键和值都可为空

  HashTable:线程安全

       键不能空,值能空

  TreeMap:自然顺序,键要兼容

       键和值都不能为空

       类必须实现Comparable接口,重写compareTo方法

  HashMap

HashMap hm=new HashMap();
hm.put("ss",15);obj类型
hm.put("ss",20);//键只有一个。每个省一个省会
hm.get(ss);//围绕key
hm.containsKey();
hm.remove();
Collection l=hm.values();//获取值

  遍历方式比较特殊,将键值对封装为一个类:hm.entrySet()方法。

Set set=hm.entrySet();
for(Object o:set){
     Entry e=(Entry)o;//利用entry类的getValue和getKey方法
     syso(e.getValue()+e.getKey);    
}
//方法2:hashMap的get方法
for(Object o:set){
     syso(o+" "+ hm.get(o));
}

  HashSet的底层是hashMap写的

三.Collection和泛型简介

  封装了许多静态方法,如add remove等

  数组的排序用Arrays.sort();

  而集合的排序用Collections.sort();

 

  泛型:用于规定集合中的数据类型

ArrayList<String> a=new ArrayList<String>();//只能放字符串到al中
HashMap<String,String>hm=new HashMap<String ,String>();

 

12集合(3)-----Map

原文:https://www.cnblogs.com/anzhilanxiao/p/10587756.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!