加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java 集合框架

发布时间:2020-12-14 06:37:45 所属栏目:Java 来源:网络整理
导读:转载地址: p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei','微软雅黑',sans-serif;" a href="http://lib.csdn.net/base/javase" rel="n

转载地址:

<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,51,51);font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei','微软雅黑',sans-serif;">
<a href="http://lib.csdn.net/base/javase" rel="nofollow" class="replace_word" title="Java SE知识库" style="text-decoration:none;color:rgb(223,52,52);font-weight:bold;">Java集合框架大致可以分为五个部分:List列表,Set集合、Map映射、迭代器、工具类


<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
List 接口通常表示一个列表(数组、队列、链表


<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
栈),其中的元素?<span style="text-indent:0px;">可以重复?的是:ArrayList 和LinkedList,另外还有不常用的Vector。LinkedList实现来Queue接口,因此也可以作为队列使用。


<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
Set接口通常表示一个集合,其中的元素?<span style="text-indent:0px;">不可以重复?(通过hashcode和equals函数保证),常用的实现类有HashSet和TreeSet


<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
Map是一个?<span style="text-indent:0px;">映射接口?,其中的每个元素都是一个?<span style="text-indent:0px;">key-value键值对?,同样抽象类AbstractMap通过适配器模式实现了Map接口中的大部分函数,TreeMap


<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
HashMap、WeakHashMap等实现类都通过继承AbstractMap实现的,另外,不常用的HashTable直接实现了Map接口。


<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
Iterator是遍历集合的迭代器(不能遍历Map,只用来遍历Collection),Collection的实现类都实现了iterator()函数,它返回一个Iterator对象,用来遍历集合,ListIterator则专门用来遍历List。


<h2 style="font-family:'Helvetica Neue',sans-serif;line-height:1.6em;color:rgb(51,51);font-size:18px;text-indent:1em;">
<a name="t0" style="color:rgb(12,137,207);">ArrayList
<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
ArrayList的内部实现是基于基础的对象数组的


<p style="font-size:16px;line-height:27.2px;text-indent:1em;color:rgb(51,sans-serif;">
特点:


<ul style="list-style-type:none;font-size:16px;line-height:27.2px;color:rgb(51,sans-serif;">
<li style="line-height:1.7em;list-style-type:disc;">顺序表
<li style="line-height:1.7em;list-style-type:disc;">大小可变
<li style="line-height:1.7em;list-style-type:disc;">允许null元素
<li style="line-height:1.7em;list-style-type:disc;">线程不同步、查询速度快、增删满

?,LinkedList中的get方法是按照顺序从列表的一端开始检查,直到另外一端。对LinkedList而言,访问列表中的某个指定元素没有更快的方法了。?它们之间最主要的区别在于ArrayList是可改变大小的数组,而LinkedList是双向链接串列(doubly LinkedList)?,因为Array是基于索引(index)的数据结构,它使用索引在数组中搜索和读取数据是很快的。Array获取数据的时间复杂度是O(1),但是要删除数据却是开销很大的,因为这需要重排数组中的所有数据。

   {
     final  N = ;
     List values;
     {
        Integer vals[] =  Integer[N];
        Random r =  Random();
         ( i = ,currval = ; i < N; i++) {
            vals[i] =  Integer(currval);
            currval += r.nextInt() + ;
        }
        values = Arrays.asList(vals);
    }
      () {
         start = System.currentTimeMillis();
        ; i < N; i++) {
             index = Collections.binarySearch(lst,values.(i));
             (index != i)
                System..println();
        }
         System.currentTimeMillis() - start;
    }
     () {
        System. + timeList( ArrayList(values)));
        System. + timeList( LinkedList(values)));
    }
}

?集合中有??,当我们提到HashSet时,第一件事情就是在将对象存储在HashSet之前, 要先确保对象??,这样才能比较对象的值是否相等,以确保set中没有储存相等的对象。如果我们没有重写这两个方法,将会使用这个方法的默认实现。

。Map接口有两个基本的实现,HashMap和TreeMap。TreeMap保存了对象的排列次序,而HashMap则不能。??HashMap是非synchronized的,但collection框架提供方法能保证HashMap synchronized,这样多个线程同时访问HashMap时,能保证只有一个线程更改Map。

的存储结构,能够快速将key的数据put方式存储起来,然后很快的通过get取出来”,“HashMap不是线程安全的, HashTable是线程安全的,通过synchronized实现的

?共同完成的

?,这意味着Hashtable是线程安全的,多个线程可以共享一个Hashtable;而如果没有正确的同步的话,多个线程是不能共享HashMap的。Java 5提供了ConcurrentHashMap,它是HashTable的替代,比HashTable的扩展性更好。另一个区别是HashMap的迭代器(Iterator)是fail-fast迭代器,而Hashtable的enumerator迭代器不是fail-fast的。所以当有其它线程改变了HashMap的结构(增加或者移除元素),将会抛出ConcurrentModificationException,但迭代器本身的remove()方法移除元素则不会抛出ConcurrentModificationException异常。但这并不是一个一定发生的行为,要看JVM。

 {
    ) {
    HashMap<String,String> hashMap = <span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;new</span> HashMap<String,String>();
    hashMap.put(<span class="hljs-string" style="color:rgb(136,0);"&gt;"cn"</span>,<span class="hljs-string" style="color:rgb(136,0);"&gt;"中国"</span>);
    hashMap.put(<span class="hljs-string" style="color:rgb(136,0);"&gt;"us"</span>,0);"&gt;"米国"</span>);
    hashMap.put(<span class="hljs-string" style="color:rgb(136,0);"&gt;"en"</span>,0);"&gt;"英国"</span>);
    hashMap.put(<span class="hljs-string" style="color:rgb(136,0);"&gt;""</span>,0);"&gt;""</span>);

    System.<span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;out</span>.println(hashMap);
    System.<span class="hljs-keyword" style="color:rgb(51,0);"&gt;"cn"</span> + hashMap.<span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;get</span>(<span class="hljs-string" style="color:rgb(136,0);"&gt;"cn"</span>));
    System.<span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;out</span>.println(hashMap.containsKey(<span class="hljs-string" style="color:rgb(136,51);font-weight:700;"&gt;out</span>.println(hashMap.keySet());
    System.<span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;out</span>.println(hashMap.isEmpty());

    Hashtable table = <span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;new</span> Hashtable();
    table.put(<span class="hljs-string" style="color:rgb(136,0);"&gt;"key1"</span>,0);"&gt;"value1"</span>);<span class="hljs-comment" style="color:rgb(136,136,136);"&gt;//键 和 值,</span>
    table.put(<span class="hljs-string" style="color:rgb(136,0);"&gt;"key2"</span>,0);"&gt;"value2"</span>);
    table.put(<span class="hljs-string" style="color:rgb(136,0);"&gt;"key3"</span>,0);"&gt;"value3"</span>);<span class="hljs-comment" style="color:rgb(136,136);"&gt;//相当于堆栈 后进先出</span>

    Enumeration e = table.keys();<span class="hljs-comment" style="color:rgb(136,136);"&gt;//创建枚举</span>
    <span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;while</span> (e.hasMoreElements()) {<span class="hljs-comment" style="color:rgb(136,136);"&gt;//是否有元素</span>
        String key = (String) e.nextElement();
        System.<span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;out</span>.println(key + <span class="hljs-string" style="color:rgb(136,0);"&gt;" : "</span> + table.<span class="hljs-keyword" style="color:rgb(51,51);font-weight:700;"&gt;get</span>(key));
    }
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读