博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java_集合框架
阅读量:4670 次
发布时间:2019-06-09

本文共 4237 字,大约阅读时间需要 14 分钟。

一、集合框架图

二、Collection接口

    Collection中可以存储的元素间
无序
可以重复的元素。
    Collection接口的子接口List和Set,Map不是Collection的子接口。

三、List接口

List接口中的元素的特点:

    List中的元素有序,可以重复。

    两个常用的实现类ArrayList和LinkedList
    
1,ArrayList
       
 类似数组形式存储,访问数度快,增删慢,线程不安全。
       
 
Vector是ArrayList的多线程的一个替代品。
        ArrayList遍历方式:
public static void main(String[] args) {     List
list=new ArrayList
(); list.add("111"); list.add("222"); list.add("333"); //第一种遍历方法使用foreach遍历List for (String str : list) {
//也可以改写for(int i=0;i
ite=list.iterator(); while(ite.hasNext()) { System.out.println(ite.next()); } }
    2
,LinkedList
        
类似链表结果,查询慢,增删快
,线程不安全。
        LinkedList遍历方式:
public static void main(String[] args) {         List
list=new LinkedList
(); list.add("111"); list.add("222"); list.add("333"); //LinkedList遍历的第一种方式使用数组的方式 String[] strArray=new String[list.size()]; list.toArray(strArray); for(String str:strArray) { System.out.println(str); } //LinkedList遍历的第二种方式 for(String str:list) { System.out.println(str); }}

四、Set接口

    Set中的元素
无序,不重复。
    虽然Set中元素没有顺序,但是元素在set中的位置是有由该元素的HashCode决定的,其具体位置其实是固定的。
    Set集合中去重和Hashcode与equals方法之间相关。
    常见实现类有HashSet,LinedHashSet和TreeSet
    
1,HashSet
        底层基于Hash算法进行存储元素,
允许null,无序,不重复,元素位置固定
        HashSet是通过HashMap实现的。
        HashSet的几种遍历方法:
public static void main(String[] args) {     Set
set=new HashSet
(); set.add("111"); set.add("222"); set.add("333"); //遍历集合的第一种方法,使用数组的方法 String[] strArray=new String[set.size()]; strArray=set.toArray(strArray); for(String str:strArray)//此处也可以使用for(int i=0;i
iterator=set.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); }}
  2
,LinkHashSet
        LinkHashSet不仅是Set接口的子接口而且还是上面HashSet接口的子接口。
        TreeSet是通过TreeMap实现的。
        LinkHashSet底层是基于LinkedHashMap来实现,和HashSet主要区别在于LinkedHashSet中存储的元素是在哈希算法的基础上增加了链式表的结构。
  3
,TreeSet
        TreeSet底层算法基于红黑树,
允许null,有序,不重复,元素位置固定
        TreeSet和HashSet的区别:
            1,HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的
            2, Map的key和Set都有一个共同的特性就是集合的唯一性.TreeMap更是多了一个排序的功能.
            3, hashCode和equal()是HashMap用的, 因为无需排序所以只需要关注定位和唯一性即可.
                a. hashCode是用来计算hash值的,hash值是用来确定hash表索引的.
                b. hash表中的一个索引处存放的是一张链表, 所以还要通过equal方法循环比较链上的每一个对象才可以真正定位到键值对应的Entry.
                c. put时,如果hash表中没定位到,就在链表前加一个Entry,如果定位到了,则更换Entry中的value,并返回旧value
            4, 由于TreeMap需要排序,所以需要一个Comparator为键值进行大小比较.当然也是用Comparator定位的.
                a. Comparator可以在创建TreeMap时指定
                b. 如果创建时没有确定,那么就会使用key.compareTo()方法,这就要求key必须实现Comparable接口.
                c. TreeMap是使用Tree数据结构实现的,所以使用compare接口就可以完成定位了.
public static void main(String[] args) {        //String实体类中实现Comparable接口,所以在初始化TreeSet的时候,        //无需传入比较器        TreeSet
treeSet=new TreeSet
(); treeSet.add("d"); treeSet.add("c"); treeSet.add("b"); treeSet.add("a"); Iterator
iterator=treeSet.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); }}

五、Map接口

    Map中的每个成员方法由一个关键字(key)和一个值(value)构成。
    常见实现类HashMap、TreeMap、LinkedHashMap、HashTable
    
1,HashMap
        HashMap无序的、不可重复、查询快、null、非线程安全。
        HashMap实现了Map、CloneMap、Serializable三个接口,并且继承自AbstractMap类。
        HashMap基于hash数组实现,若key的hash值相同则使用链表方式进行保存。
        HashMap遍历方式
public static void main(String[] args) {        //方式1        Map map = new HashMap();        map.put("A", "1233");        map.put("B", "12334");        map.put("C", "12334");        Iterator iter = map.entrySet().iterator();        while (iter.hasNext()) {            Map.Entry entry = (Map.Entry) iter.next();            System.out.println(entry.getKey() + "--" + entry.getValue());        }        //方式2效率高        iter = map.keySet().iterator();          while (iter.hasNext()) {              Object key = iter.next();            System.out.println(key + "--" + map.get(key));        }      }
  2
,TreeMap
        TreeMap有序的、不可重复、遍历快、允许null、非线程安全。
        HashMap基于红黑树实现。
    3
,LinkedHashMap
        LinkedHashMap有序的、不可重复、遍历快、允许null、非线程安全。
        LinkedHashMap输出顺序和输入顺序相同。
        LinkedHashMap继承hashMap,底层存储结果是Hashmap的table
    4
,Hashtable
        Hashtable有序的、不可重复、不允许null、线程安全。

转载于:https://www.cnblogs.com/my-haohao/p/5659104.html

你可能感兴趣的文章
关于bash中if语法结构的广泛误解(转)
查看>>
10G整数文件中寻找中位数或者第K大数
查看>>
操作手机数据库的uri
查看>>
Python小应用1 - 抓取网页中的链接地址
查看>>
HTML表格和列表笔记&练习<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>关于表格的一些练...
查看>>
Hadoop HBase概念学习系列之hbase shell中执行java方法(高手必备)(二十五)
查看>>
数据类型
查看>>
SharePoint 2010中的内容类型集线器 - 内容类型发布与订阅
查看>>
如何解决在Windows Server 2008 R2 上安装证书服务重启后出现 CertificationAuthority 91错误事件...
查看>>
c# 获取键盘的输入
查看>>
mysql忘记密码
查看>>
小股神助A股股民畅享经济发展红利
查看>>
Python灰帽子pdf
查看>>
Node.js区块链开发pdf
查看>>
轻松学SQL Server数据库pdf
查看>>
Oracle 日期查询
查看>>
说说今年的计划
查看>>
把discuzX 的用户登录信息添加到纯静态页面
查看>>
文件大小计算
查看>>
iOS:给图片置灰色
查看>>