欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java回顧之集合的總結(jié)概述

 更新時(shí)間:2013年05月08日 09:49:35   作者:  
在這篇文章里,我們關(guān)注Java中的集合(Collection)。集合是編程語(yǔ)言中基礎(chǔ)的一部分,Java自JDK早期,就引入了Java Collection Framework。設(shè)計(jì)JCF的那個(gè)人,后來(lái)還寫了一本書,叫《Effective Java》

Java中的集合主要集中在2部分,一部分是java.util包中,一部分是java.util.concurrent中,后者是在前者的基礎(chǔ)上,定義了一些實(shí)現(xiàn)了同步功能的集合。

這篇文章主要關(guān)注java.util下的各種集合對(duì)象。Java中的集合對(duì)象可以粗略的分為3類:List、Set和Map。對(duì)應(yīng)的UML圖如下(包括了java.util下大部分的集合對(duì)象):

Collection概述

Java集合中的List和Set都從Collection出來(lái),它是一個(gè)學(xué)習(xí)集合很不錯(cuò)的入口,它包含了集合中通常需要有的操作:

    添加元素:add/addAll
    清空集合:clear
    刪除元素:remove/removeAll
    判斷集合中是否包含某元素:contains/containsAll
    判斷集合是否為空:isEmpty
    計(jì)算集合中元素的個(gè)數(shù):size
    將集合轉(zhuǎn)換為數(shù)組:toArray
    獲取迭代器:iterator

我們來(lái)看一個(gè)簡(jiǎn)單的例子,下面的代碼會(huì)返回一個(gè)集合,集合中的元素是隨機(jī)生成的整數(shù):

復(fù)制代碼 代碼如下:

private static Collection initCollection()
 {
     Collection<Integer> collection = new ArrayList<Integer>();
     Random r = new Random();
     for (int i = 0 ; i < 5; i++)
     {
         collection.add(new Integer(r.nextInt(100)));
     }

     return collection;
 }

在對(duì)集合進(jìn)行操作的過(guò)程中,遍歷是一個(gè)經(jīng)常使用的操作,我們可以使用兩種方式對(duì)集合進(jìn)行遍歷:

1) 使用迭代器對(duì)集合進(jìn)行遍歷。正如上面描述Collection接口時(shí)所說(shuō),所有集合都會(huì)有一個(gè)迭代器,我們可以用它來(lái)遍歷集合。

復(fù)制代碼 代碼如下:

private static void accessCollectionByIterator(Collection<Integer> collection)
 {
     Iterator<Integer> iterator = collection.iterator();
     System.out.println("The value in the list:");
     while(iterator.hasNext())
     {
         System.out.println(iterator.next());
     }
 }

2)使用foreach遍歷集合。
復(fù)制代碼 代碼如下:

private static void accessCollectionByFor(Collection<Integer> collection)
 {
     System.out.println("The value in the list:");
     for(Integer value : collection)
     {
         System.out.println(value);
     }
 }

List

Java中的List是對(duì)數(shù)組的有效擴(kuò)展,它是這樣一種結(jié)構(gòu),如果不使用泛型,它可以容納任何類型的元素,如果使用泛型,那么它只能容納泛型指定的類型的元素。和數(shù)組相比,List的容量是可以動(dòng)態(tài)擴(kuò)展的。

List中的元素是可以重復(fù)的,里面的元素是“有序”的,這里的“有序”,并不是排序的意思,而是說(shuō)我們可以對(duì)某個(gè)元素在集合中的位置進(jìn)行指定。

List中常用的集合對(duì)象包括:ArrayList、Vector和LinkedList,其中前兩者是基于數(shù)組來(lái)進(jìn)行存儲(chǔ),后者是基于鏈表進(jìn)行存儲(chǔ)。其中Vector是線程安全的,其余兩個(gè)不是線程安全的。

List中是可以包括null的,即使是使用了泛型。

ArrayList可能是我們平時(shí)用到的最多的集合對(duì)象了,在上述的示例代碼中,我們也是使用它來(lái)實(shí)例化一個(gè)Collection對(duì)象,在此不再贅述。
Vector

Vector的示例如下,首先我們看如何生成和輸出Vector:

復(fù)制代碼 代碼如下:

private static void vectorTest1()
 {
     List<Integer> list = new Vector<Integer>();
     for (int i = 0 ; i < 5; i++)
     {
         list.add(new Integer(100));
     }
     list.add(null);
     System.out.println("size of vector is " + list.size());
     System.out.println(list);
 }

它的元素中,既包括了重復(fù)元素,也包括了null,輸出結(jié)果如下:
復(fù)制代碼 代碼如下:

size of vector is 6
[100, 100, 100, 100, 100, null]

下面的示例,演示了Vector中的一些常用方法:
復(fù)制代碼 代碼如下:

private static void vectorTest2()
 {
     Vector<Integer> list = new Vector<Integer>();
     Random r = new Random();
     for (int i = 0 ; i < 10; i++)
     {
         list.add(new Integer(r.nextInt(100)));
     }
     System.out.println("size of vector is " + list.size());
     System.out.println(list);
     System.out.println(list.firstElement());
     System.out.println(list.lastElement());
     System.out.println(list.subList(3, 8));
     List<Integer> temp = new ArrayList<Integer>();
     for(int i = 4; i < 7; i++)
     {
         temp.add(list.get(i));
     }
     list.retainAll(temp);
     System.out.println("size of vector is " + list.size());
     System.out.println(list);
 }

它的輸出結(jié)果如下:
復(fù)制代碼 代碼如下:

size of vector is 10
[39, 41, 20, 9, 29, 32, 54, 12, 94, 82]


[9, 29, 32, 54, 12]
size of vector is 3
[29, 32, 54]


LinkedList

LinkedList使用鏈表來(lái)存儲(chǔ)數(shù)據(jù),它的示例代碼如下:

復(fù)制代碼 代碼如下:

LinkedList示例
 private static void linkedListTest1()
 {
     LinkedList<Integer> list = new LinkedList<Integer>();
     Random r = new Random();
     for (int i = 0 ; i < 10; i++)
     {
         list.add(new Integer(r.nextInt(100)));
     }
     list.add(null);
     System.out.println("size of linked list is " + list.size());
     System.out.println(list);
     System.out.println(list.element());
     System.out.println(list.getFirst());
     System.out.println(list.getLast());
     System.out.println(list.peek());
     System.out.println(list.peekFirst());
     System.out.println(list.peekLast());
     System.out.println(list.poll());
     System.out.println(list.pollFirst());
     System.out.println(list.pollLast());
     System.out.println(list.pop());
     list.push(new Integer(100));
     System.out.println("size of linked list is " + list.size());
     System.out.println(list);
 }

這里列出了LinkedList常用的各個(gè)方法,從方法名可以看出,LinkedList也可以用來(lái)實(shí)現(xiàn)棧和隊(duì)列。

輸出結(jié)果如下:

復(fù)制代碼 代碼如下:

size of linked list is 11
[17, 21, 5, 84, 19, 57, 68, 26, 27, 47, null]


null


null


null

size of linked list is 8
[100, 84, 19, 57, 68, 26, 27, 47]


Set

Set 和List類似,都是用來(lái)存儲(chǔ)單個(gè)元素,單個(gè)元素的數(shù)量不確定。但Set不能包含重復(fù)元素,如果向Set中插入兩個(gè)相同元素,那么后一個(gè)元素不會(huì)被插入。

Set可以大致分為兩類:不排序Set和排序Set,不排序Set包括HashSet和LinkedHashSet,排序Set主要指TreeSet。其中HashSet和LinkedHashSet可以包含null。
HashSet

HashSet是由Hash表支持的一種集合,它不是線程安全的。

我們來(lái)看下面的示例,它和Vector的第一個(gè)示例基本上是相同的:

復(fù)制代碼 代碼如下:

private static void hashSetTest1()
 {
     Set<Integer> set = new HashSet<Integer>();

     for (int i = 0; i < 3; i++)
     {
         set.add(new Integer(100));
     }
     set.add(null);

     System.out.println("size of set is " + set.size());
     System.out.println(set);
 }

這里,HashSet中既包含了重復(fù)元素,又包含了null,和Vector不同,這里的輸出結(jié)果如下:
復(fù)制代碼 代碼如下:

size of set is 2
[null, 100]

對(duì)于HashSet是如何判斷兩個(gè)元素是否是重復(fù)的,我們可以深入考察一下。Object中也定義了equals方法,對(duì)于HashSet中的元素,它是根據(jù)equals方法來(lái)判斷元素是否相等的,為了證明這一點(diǎn),我們可以定義個(gè)“不正?!钡念愋停?BR>
復(fù)制代碼 代碼如下:

定義MyInteger對(duì)象

class MyInteger
{
    private Integer value;

    public MyInteger(Integer value)
    {
        this.value = value;
    }

    public String toString()
    {
        return String.valueOf(value);
    }

    public int hashCode()
    {
        return 1;
    }

    public boolean equals(Object obj)
    {
        return true;
    }
}


可以看到,對(duì)于MyInteger來(lái)說(shuō),對(duì)于任意兩個(gè)實(shí)例,我們都認(rèn)為它是不相等的。

下面是對(duì)應(yīng)的測(cè)試方法:

復(fù)制代碼 代碼如下:

private static void hashSetTest2()
 {
     Set<MyInteger> set = new HashSet<MyInteger>();

     for (int i = 0; i < 3; i++)
     {
         set.add(new MyInteger(100));
     }

     System.out.println("size of set is " + set.size());
     System.out.println(set);
 }

它的輸出結(jié)果如下:
復(fù)制代碼 代碼如下:

size of set is 3
[100, 100, 100]

可以看到,現(xiàn)在HashSet里有“重復(fù)”元素了,但對(duì)于MyInteger來(lái)說(shuō),它們不是“相同”的。
TreeSet

TreeSet是支持排序的一種Set,它的父接口是SortedSet。

我們首先來(lái)看一下TreeSet都有哪些基本操作:

復(fù)制代碼 代碼如下:

private static void treeSetTest1()
 {
     TreeSet<Integer> set = new TreeSet<Integer>();

     Random r = new Random();
     for (int i = 0 ; i < 5; i++)
     {
         set.add(new Integer(r.nextInt(100)));
     }

     System.out.println(set);
     System.out.println(set.first());
     System.out.println(set.last());
     System.out.println(set.descendingSet());
     System.out.println(set.headSet(new Integer(50)));
     System.out.println(set.tailSet(new Integer(50)));
     System.out.println(set.subSet(30, 60));
     System.out.println(set.floor(50));
     System.out.println(set.ceiling(50));
 }

它的輸出結(jié)果如下:
復(fù)制代碼 代碼如下:

[8, 42, 48, 49, 53]


[53, 49, 48, 42, 8]
[8, 42, 48, 49]
[53]
[42, 48, 49, 53]


TreeSet中的元素,一般都實(shí)現(xiàn)了Comparable接口,默認(rèn)情況下,對(duì)于Integer來(lái)說(shuō),SortedList是采用升序來(lái)存儲(chǔ)的,我們也可以自定義Compare方式,例如以降序的方式來(lái)存儲(chǔ)。

下面,我們首先重新定義Integer:

復(fù)制代碼 代碼如下:

定義MyInteger2對(duì)象
 class MyInteger2 implements Comparable
 {
     public int value;

     public MyInteger2(int value)
     {
         this.value = value;
     }

     public int compareTo(Object arg0)
     {
         MyInteger2 temp = (MyInteger2)arg0;
         if (temp == null) return -1;
         if (temp.value > this.value)
         {
             return 1;
         }
         else if (temp.value < this.value)
         {
             return -1;
         }
         return 0;
     }

     public boolean equals(Object obj)
     {
         return compareTo(obj) == 0;
     }

     public String toString()
     {
         return String.valueOf(value);
     }
 }

下面是測(cè)試代碼:
復(fù)制代碼 代碼如下:

private static void treeSetTest2()
 {
     TreeSet<Integer> set1 = new TreeSet<Integer>();
     TreeSet<MyInteger2> set2 = new TreeSet<MyInteger2>();
     Random r = new Random();
     for (int i = 0 ; i < 5; i++)
     {
         int value = r.nextInt(100);
         set1.add(new Integer(value));
         set2.add(new MyInteger2(value));
     }
     System.out.println("Set1 as below:");
     System.out.println(set1);
     System.out.println("Set2 as below:");
     System.out.println(set2);
 }

代碼的運(yùn)行結(jié)果如我們所預(yù)期的那樣,如下所示:
復(fù)制代碼 代碼如下:

Set1 as below:
[13, 41, 42, 45, 61]
Set2 as below:
[61, 45, 42, 41, 13]

Map

Map中存儲(chǔ)的是“鍵值對(duì)”,和Set類似,Java中的Map也有兩種:排序的和不排序的,不排序的包括HashMap、Hashtable和LinkedHashMap,排序的包括TreeMap。
非排序Map

HashMap和Hashtable都是采取Hash表的方式進(jìn)行存儲(chǔ),HashMap不是線程安全的,Hashtable是線程安全的,我們可以把HashMap看做是“簡(jiǎn)化”版的Hashtable。

HashMap是可以存儲(chǔ)null的,無(wú)論是對(duì)Key還是對(duì)Value。Hashtable是不可以存儲(chǔ)null的。

無(wú)論HashMap還是Hashtable,我們觀察它的構(gòu)造函數(shù),就會(huì)發(fā)現(xiàn)它可以有兩個(gè)參數(shù):initialCapacity和loadFactor,默認(rèn)情況下,initialCapacity等于16,loadFactor等于0.75。這和Hash表中可以存放的元素?cái)?shù)目有關(guān)系,當(dāng)元素?cái)?shù)目超過(guò)initialCapacity*loadFactor時(shí),會(huì)觸發(fā)rehash方法,對(duì)hash表進(jìn)行擴(kuò)容。如果我們需要向其中插入過(guò)多元素,需要適當(dāng)調(diào)整這兩個(gè)參數(shù)。

我們首先來(lái)看HashMap的示例:

復(fù)制代碼 代碼如下:

private static void hashMapTest1()
 {
     Map<Integer,String> map = new HashMap<Integer, String>();

     map.put(new Integer(1), "a");
     map.put(new Integer(2), "b");
     map.put(new Integer(3), "c");

     System.out.println(map);
     System.out.println(map.entrySet());
     System.out.println(map.keySet());
     System.out.println(map.values());
 }

這會(huì)輸出HashMap里的元素信息,如下所示。
復(fù)制代碼 代碼如下:

{1=a, 2=b, 3=c}
[1=a, 2=b, 3=c]
[1, 2, 3]
[a, b, c]

下面的示例是對(duì)null的演示:
復(fù)制代碼 代碼如下:

private static void hashMapTest2()
 {
     Map<Integer,String> map = new HashMap<Integer, String>();

     map.put(null, null);
     map.put(null, null);
     map.put(new Integer(4), null);
     map.put(new Integer(5), null);

     System.out.println(map);
     System.out.println(map.entrySet());
     System.out.println(map.keySet());
     System.out.println(map.values());
 }

執(zhí)行結(jié)果如下:
復(fù)制代碼 代碼如下:

{null=null, 4=null, 5=null}
[null=null, 4=null, 5=null]
[null, 4, 5]
[null, null, null]

接下來(lái)我們演示Hashtable,和上述兩個(gè)示例基本上完全一樣(代碼不再展開(kāi)):
復(fù)制代碼 代碼如下:

Hashtable示例
 private static void hashTableTest1()
 {
     Map<Integer,String> table = new Hashtable<Integer, String>();

     table.put(new Integer(1), "a");
     table.put(new Integer(2), "b");
     table.put(new Integer(3), "c");

     System.out.println(table);
     System.out.println(table.entrySet());
     System.out.println(table.keySet());
     System.out.println(table.values());
 }

 private static void hashTableTest2()
 {
     Map<Integer,String> table = new Hashtable<Integer, String>();

     table.put(null, null);
     table.put(null, null);
     table.put(new Integer(4), null);
     table.put(new Integer(5), null);

     System.out.println(table);
     System.out.println(table.entrySet());
     System.out.println(table.keySet());
     System.out.println(table.values());
 }

執(zhí)行結(jié)果如下:
復(fù)制代碼 代碼如下:

{3=c, 2=b, 1=a}
[3=c, 2=b, 1=a]
[3, 2, 1]
[c, b, a]
Exception in thread "main" java.lang.NullPointerException
    at java.util.Hashtable.put(Unknown Source)
    at sample.collections.MapSample.hashTableTest2(MapSample.java:61)
    at sample.collections.MapSample.main(MapSample.java:11)

可以很清楚的看到,當(dāng)我們?cè)噲D將null插入到hashtable中時(shí),報(bào)出了空指針異常。
排序Map

排序Map主要是指TreeMap,它對(duì)元素增、刪、查操作時(shí)的時(shí)間復(fù)雜度都是O(log(n))。它不是線程安全的。

它的特點(diǎn)和TreeSet非常像,這里不再贅述。

相關(guān)文章

  • Springboot?整合?RocketMQ?收發(fā)消息的配置過(guò)程

    Springboot?整合?RocketMQ?收發(fā)消息的配置過(guò)程

    這篇文章主要介紹了Springboot?整合?RocketMQ?收發(fā)消息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • java 中file.encoding的設(shè)置詳解

    java 中file.encoding的設(shè)置詳解

    這篇文章主要介紹了java 中file.encoding的設(shè)置詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    這篇文章主要介紹了Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例,從創(chuàng)建一個(gè)web項(xiàng)目開(kāi)始,分享了項(xiàng)目結(jié)構(gòu)以及具體Java代碼和前端頁(yè)面等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例

    spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例

    這篇文章主要介紹了spring使用ehcache實(shí)現(xiàn)頁(yè)面緩存示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • Java中實(shí)現(xiàn)二叉樹(shù)的遍歷與重構(gòu)

    Java中實(shí)現(xiàn)二叉樹(shù)的遍歷與重構(gòu)

    這篇文章主要介紹了Java中實(shí)現(xiàn)二叉樹(shù)的遍歷與重構(gòu),樹(shù)是一種非線性的數(shù)據(jù)結(jié)構(gòu),它是由n(n>=0)個(gè)有限結(jié)點(diǎn)組成一個(gè)具有層次關(guān)系的集合,把它叫做樹(shù)是因?yàn)樗雌饋?lái)像一棵倒掛的樹(shù),也就是說(shuō)它是根朝上,而葉朝下的,需要的朋友可以參考下
    2023-10-10
  • 詳解Java高階語(yǔ)法Volatile

    詳解Java高階語(yǔ)法Volatile

    這篇文章主要介紹了Java高階語(yǔ)法Volatile,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)

    java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Springcloud中Feign傳遞參數(shù)的過(guò)程解析

    Springcloud中Feign傳遞參數(shù)的過(guò)程解析

    這篇文章主要介紹了Springcloud中Feign傳遞參數(shù)的過(guò)程,單個(gè)參數(shù)的傳值有兩種方式,第一種使用@RequestParam/@PathVariable進(jìn)行傳值,傳遞多個(gè)參數(shù):多個(gè)參數(shù)的傳值可以使用多個(gè)@RequestParam來(lái)進(jìn)行傳參,需要的朋友可以參考下
    2023-09-09
  • Mybatis-Mapper.xml文件前綴詳解

    Mybatis-Mapper.xml文件前綴詳解

    這篇文章主要介紹了Mybatis-Mapper.xml文件前綴,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java 文件流的處理方式 文件打包成zip

    java 文件流的處理方式 文件打包成zip

    這篇文章主要介紹了java 文件流的處理方式 文件打包成zip,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論