麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

Java 集合類詳解

2019-11-06 06:11:12
字體:
來源:轉載
供稿:網友

學習java的同學注意了!!! 學習過程中遇到什么問題或者想獲取學習資源的話,歡迎加入Java學習交流群,群號碼:523047986  我們一起學Java!

1.java集合類圖

1.1

1.2

  上述類圖中,實線邊框的是實現類,比如ArrayList,LinkedList,HashMap等,折線邊框的是抽象類,比如AbstractCollection,AbstractList,AbstractMap等,而點線邊框的是接口,比如Collection,Iterator,List等。

  發現一個特點,上述所有的集合類,都實現了Iterator接口,這是一個用于遍歷集合中元素的接口,主要包含hashNext(),next(),remove()三種方法。它的一個子接口LinkedIterator在它的基礎上又添加了三種方法,分別是add(),PRevious(),hasprevious()。也就是說如果是先Iterator接口,那么在遍歷集合中元素的時候,只能往后遍歷,被遍歷后的元素不會在遍歷到,通常無序集合實現的都是這個接口,比如HashSet,HashMap;而那些元素有序的集合,實現的一般都是LinkedIterator接口,實現這個接口的集合可以雙向遍歷,既可以通過next()訪問下一個元素,又可以通過previous()訪問前一個元素,比如ArrayList。

  還有一個特點就是抽象類的使用。如果要自己實現一個集合類,去實現那些抽象的接口會非常麻煩,工作量很大。這個時候就可以使用抽象類,這些抽象類中給我們提供了許多現成的實現,我們只需要根據自己的需求重寫一些方法或者添加一些方法就可以實現自己需要的集合類,工作流昂大大降低。

1.3

2.詳解

 2.1HashSet

HashSet是Set接口的一個子類,主要的特點是:里面不能存放重復元素,而且采用散列的存儲方法,所以沒有順序。這里所說的沒有順序是指:元素插入的順序與輸出的順序不一致。

代碼實例:HashSetDemo

復制代碼
package edu.sjtu.erplab.collection;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class HashSetDemo {    public static void main(String[] args) {        Set<String> set=new HashSet<String>();                set.add("a");        set.add("b");        set.add("c");        set.add("c");        set.add("d");                //使用Iterator輸出集合        Iterator<String> iter=set.iterator();        while(iter.hasNext())        {            System.out.print(iter.next()+" ");        }        System.out.println();        //使用For Each輸出結合        for(String e:set)        {            System.out.print(e+" ");        }        System.out.println();                //使用toString輸出集合        System.out.println(set);    }}復制代碼

代碼實例:SetTest

復制代碼
package edu.sjtu.erplab.collection;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.util.HashSet;import java.util.Iterator;import java.util.Scanner;import java.util.Set;public class SetTest {    public static void main(String[] args) throws FileNotFoundException {        Set<String> Words=new HashSet<String>();        //通過輸入流代開文獻        //方法1:這個方法不需要拋出異常        InputStream inStream=SetTest.class.getResourceAsStream("Alice.txt");                //方法2:這個方法需要拋出異常        //InputStream inStream = new FileInputStream("D://Documents//workspace//JAVAStudy//src//edu//sjtu//erplab//collection//Alice.txt");        Scanner in=new Scanner(inStream);        while(in.hasNext())        {            words.add(in.next());        }                Iterator<String> iter=words.iterator();                for(int i=0;i<5;i++)        {            if(iter.hasNext())            System.out.println(iter.next());        }                System.out.println(words.size());    }}復制代碼

 

2.2ArrayList

ArrayList是List的子類,它和HashSet想法,允許存放重復元素,因此有序。集合中元素被訪問的順序取決于集合的類型。如果對ArrayList進行訪問,迭代器將從索引0開始,每迭代一次,索引值加1。然而,如果訪問HashSet中的元素,每個元素將會按照某種隨機的次序出現。雖然可以確定在迭代過程中能夠遍歷到集合中的所有元素,但卻無法預知元素被訪問的次序。

代碼實例:ArrayListDemo

復制代碼
package edu.sjtu.erplab.collection;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class ArrayListDemo {    public static void main(String[] args) {        List<String> arrList=new ArrayList<String>();                arrList.add("a");        arrList.add("b");        arrList.add("c");        arrList.add("c");        arrList.add("d");        //使用Iterator輸出集合        Iterator<String> iter=arrList.iterator();        while(iter.hasNext())        {            System.out.print(iter.next()+" ");        }        System.out.println();        //使用For Each輸出結合        for(String e:arrList)        {            System.out.print(e+" ");        }        System.out.println();                //使用toString輸出集合        System.out.println(arrList);    }}復制代碼

2.3 ListIterator

ListIterator是一種可以在任何位置進行高效地插入和刪除操作的有序序列。

代碼實例:LinkedListTest

復制代碼
package edu.sjtu.erplab.collection;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.ListIterator;public class LinkedListTest {    public static void main(String[] args) {        List<String> a=new ArrayList<String>();        a.add("a");        a.add("b");        a.add("c");        System.out.println(a);                List<String> b=new ArrayList<String>();        b.add("d");        b.add("e");        b.add("f");        b.add("g");        System.out.println(b);                //ListIterator在Iterator基礎上添加了add(),previous()和hasPrevious()方法        ListIterator<String> aIter=a.listIterator();        //普通的Iterator只有三個方法,hasNext(),next()和remove()        Iterator<String> bIter=b.iterator();                //b歸并入a當中,間隔交叉得插入b中的元素        while(bIter.hasNext())        {            if(aIter.hasNext())                aIter.next();            aIter.add(bIter.next());        }        System.out.println(a);                //在b中每隔兩個元素刪除一個        bIter=b.iterator();                while(bIter.hasNext())        {            bIter.next();            if(bIter.hasNext())            {                bIter.next();//remove跟next是成對出現的,remove總是刪除前序                bIter.remove();            }        }        System.out.println(b);                //刪除a中所有的b中的元素        a.removeAll(b);        System.out.println(a);    }}復制代碼

 2.4HashMap

參考之前的一篇博客:Hashmap實現原理

 

 2.5WeekHashMapDemo

復制代碼
package edu.sjtu.erplab.collection;import java.util.WeakHashMap;public class WeekHashMapDemo {    public static void main(String[] args) {        int size = 100;        if (args.length > 0) {            size = Integer.parseInt(args[0]);        }        Key[] keys = new Key[size];        WeakHashMap<Key, Value> whm = new WeakHashMap<Key, Value>();        for (int i = 0; i < size; i++) {            Key k = new Key(Integer.toString(i));            Value v = new Value(Integer.toString(i));            if (i % 3 == 0) {                keys[i] = k;//強引用            }            whm.put(k, v);//所有鍵值放入WeakHashMap中        }        System.out.println(whm);        System.out.println(whm.size());        System.gc();                try {            // 把處理器的時間讓給垃圾回收器進行垃圾回收            Thread.sleep(4000);        } catch (InterruptedException e) {            e.printStackTrace();        }                 System.out.println(whm);        System.out.println(whm.size());    }}class Key {    String id;    public Key(String id) {        this.id = id;    }    public String toString() {        return id;    }    public int hashCode() {        return id.hashCode();    }    public boolean equals(Object r) {        return (r instanceof Key) && id.equals(((Key) r).id);    }    public void finalize() {        System.out.println("Finalizing Key " + id);    }}class Value {    String id;    public Value(String id) {        this.id = id;    }    public String toString() {        return id;    }    public void finalize() {        System.out.println("Finalizing Value " + id);    }}復制代碼

輸出結果

復制代碼
{50=50, 54=54, 53=53, 52=52, 51=51, 46=46, 47=47, 44=44, 45=45, 48=48, 49=49, 61=61, 60=60, 63=63, 62=62, 65=65, 64=64, 55=55, 56=56, 57=57, 58=58, 59=59, 76=76, 75=75, 74=74, 73=73, 72=72, 71=71, 70=70, 68=68, 69=69, 66=66, 67=67, 85=85, 84=84, 87=87, 86=86, 81=81, 80=80, 83=83, 82=82, 77=77, 78=78, 79=79, 89=89, 88=88, 10=10, 90=90, 91=91, 92=92, 93=93, 94=94, 95=95, 96=96, 97=97, 98=98, 99=99, 20=20, 21=21, 12=12, 11=11, 14=14, 13=13, 16=16, 15=15, 18=18, 17=17, 19=19, 8=8, 9=9, 31=31, 4=4, 32=32, 5=5, 6=6, 30=30, 7=7, 0=0, 1=1, 2=2, 3=3, 29=29, 28=28, 27=27, 26=26, 25=25, 24=24, 23=23, 22=22, 40=40, 41=41, 42=42, 43=43, 38=38, 37=37, 39=39, 34=34, 33=33, 36=36, 35=35}100Finalizing Key 98Finalizing Key 97Finalizing Key 95Finalizing Key 94Finalizing Key 92Finalizing Key 91Finalizing Key 89Finalizing Key 88Finalizing Key 86Finalizing Key 85Finalizing Key 83Finalizing Key 82Finalizing Key 80Finalizing Key 79Finalizing Key 77Finalizing Key 76Finalizing Key 74Finalizing Key 73Finalizing Key 71Finalizing Key 70Finalizing Key 68Finalizing Key 67Finalizing Key 65Finalizing Key 64Finalizing Key 62Finalizing Key 61Finalizing Key 59Finalizing Key 58Finalizing Key 56Finalizing Key 55Finalizing Key 53Finalizing Key 52Finalizing Key 50Finalizing Key 49Finalizing Key 47Finalizing Key 46Finalizing Key 44Finalizing Key 43Finalizing Key 41Finalizing Key 40Finalizing Key 38Finalizing Key 37Finalizing Key 35Finalizing Key 34Finalizing Key 32Finalizing Key 31Finalizing Key 29Finalizing Key 28Finalizing Key 26Finalizing Key 25Finalizing Key 23Finalizing Key 22Finalizing Key 20Finalizing Key 19Finalizing Key 17Finalizing Key 16Finalizing Key 14Finalizing Key 13Finalizing Key 11Finalizing Key 10Finalizing Key 8Finalizing Key 7Finalizing Key 5Finalizing Key 4Finalizing Key 2Finalizing Key 1{54=54, 51=51, 45=45, 48=48, 60=60, 63=63, 57=57, 75=75, 72=72, 69=69, 66=66, 84=84, 87=87, 81=81, 78=78, 90=90, 93=93, 96=96, 99=99, 21=21, 12=12, 15=15, 18=18, 9=9, 6=6, 30=30, 0=0, 3=3, 27=27, 24=24, 42=42, 39=39, 33=33, 36=36}34復制代碼

 疑問:為什么value沒有被回收。

 

3.比較

  是否有序是否允許元素重復
Collection
List
SetAbstractSet
 HashSet
 TreeSet是(用二叉排序樹)
MapAbstractMap使用key-value來映射和存儲數據,key必須唯一,value可以重復
 HashMap
 TreeMap是(用二叉排序樹)

學習Java的同學注意了!!! 學習過程中遇到什么問題或者想獲取學習資源的話,歡迎加入Java學習交流群,群號碼:523047986  我們一起學Java!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 午夜视频大全 | a黄色片| 久久国产精品91 | 视频一区 日韩 | 欧美成人精品欧美一级乱黄 | 黄色av.com | 天堂成人国产精品一区 | 99成人精品视频 | 欧美激情猛片xxxⅹ大3 | 中文字幕在线播放一区 | 久久免费视频一区 | 成人毛片视频在线播放 | 国产亚洲精品久久午夜玫瑰园 | 欧美一级淫片免费播放口 | 操嫩草 | 中文字幕欧美日韩 | 空姐一级毛片 | 黄色免费电影网址 | 国产免费一级淫片a级中文 99国产精品自拍 | 黄色大片在线免费观看 | 国产日韩a| 高颜值美女啪啪 | 久久久久久久久久久久久久国产 | 性欧美xxxx极品摘花 | 在线中文字幕播放 | 国产91对白叫床清晰播放 | 日本成人在线免费 | 欧美成人午夜一区二区三区 | 牛牛热这里只有精品 | 黄色试看视频 | 日韩视频精品 | 一区二区三区手机在线观看 | 精品一区二区三区在线播放 | 欧美在线中文字幕 | 最新一级毛片 | 在线a视频| 国av在线| 色播视频网站 | 国产精品久久久久久久久久久久午夜 | 国产精品亚洲欧美一级在线 | 久草成人在线 |