日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

集合(Collections)

發(fā)布時(shí)間:2023/12/8 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合(Collections) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 1. 概念
  • 2. Collection接口
    • 2.1. List
      • 2.1.1 源碼分析
    • 2.2. Set
  • 3. Map接口
    • 3.1. 源碼分析
  • 4. Collections工具類(lèi)

1. 概念

因?yàn)閿?shù)組存在弊端,為了方便多個(gè)對(duì)象的內(nèi)存存儲(chǔ),可以動(dòng)態(tài)的把多個(gè)對(duì)象的引用放入容器中

  • 初始化以后,其長(zhǎng)度就不可修改。
  • 數(shù)組中提供的方法非常有限,對(duì)添加、刪除、插入數(shù)據(jù)等操作,非常不便,同時(shí)效率不高。
  • 獲取數(shù)組實(shí)際元素的個(gè)數(shù)的需求,數(shù)組沒(méi)有現(xiàn)成的屬或方法可用
  • 數(shù)組存儲(chǔ)數(shù)據(jù)的特點(diǎn):有序、可重復(fù),對(duì)于無(wú)序、不可重復(fù)的需求,不能滿足

Java中,集合類(lèi)的基本接口是Collection接口和Map接口

2. Collection接口

單列數(shù)據(jù)

2.1. List

有序、可重復(fù) (Vector、ArrayList、LinkedList)

  • Vector:List 接口的古老的實(shí)現(xiàn)類(lèi),線程安全,效率低,底層使用Object[ ]數(shù)組存儲(chǔ)

  • ArrayList:List 接口的主要實(shí)現(xiàn)類(lèi),線程不安全,效率低,底層使用Object[ ]數(shù)組存儲(chǔ)

  • LinkedList:底層使用雙向鏈表存儲(chǔ),對(duì)于頻繁的插入刪除操作使用此類(lèi)效率比較高

  • public static void main(String[] args) {Collection collection = new ArrayList();collection.add("AA");collection.add("BB");collection.add(123);//自動(dòng)裝箱collection.add(new Date());Collection collection1 = new ArrayList();collection1.add("CC");collection1.add(456);//自動(dòng)裝箱collection1.add(new Persion("Tom",20));//添加其它集合collection.addAll(collection1);//判斷當(dāng)前集合中是否存在相應(yīng)的對(duì)象值System.out.println(collection.contains(456));System.out.println("***此時(shí)需重寫(xiě)Persion類(lèi)中的equals方法才是true***"+collection.contains(new Persion("Tom", 20)));Collection collection2 = new ArrayList();collection2.add("CC");collection2.add(456);collection2.add(new Persion("Tom",20));System.out.println("判斷collection2中的所有對(duì)象是否在collection1中"+collection1.containsAll(collection2));System.out.println("返回是否移除數(shù)據(jù)成功"+collection2.remove(456));System.out.println(collection2);System.out.println(collection.size());//獲取添加的元素的個(gè)數(shù)//判斷當(dāng)前集合是否為空System.out.println(collection.isEmpty());//falsecollection.clear();//清空集合元素System.out.println(collection.isEmpty());//true} Collection collection = new ArrayList();collection.add("AA");collection.add("BB");collection.add(123);collection.add(new Date());// Iterator迭代器接口實(shí)現(xiàn)集合元素遍歷,每次調(diào)用都需要新創(chuàng)建iterator對(duì)象Iterator iterator = collection.iterator();for (int i = 0; i < collection.size(); i++) {System.out.println(iterator.next());} // hasNext指針下移,判斷是否還存在下一個(gè)元素while (iterator.hasNext()){System.out.println("開(kāi)發(fā)中常用"+iterator.next());}// 集合--->數(shù)組for (Object o : collection.toArray()) {System.out.println(o);} // 數(shù)組--->集合String[] strs = {"AA","BB","CC"};List<String> list = Arrays.asList(strs);System.out.println(list);

    2.1.1 源碼分析

    // 底層創(chuàng)建長(zhǎng)度為10的Object[] elementData ArrayList arrayList = new ArrayList(); arrayList.add(123);//相當(dāng)于elementData[0] = new Integer(123); arrayList.add(123456);//若此時(shí)elementData數(shù)組長(zhǎng)度不夠,則按照原容量的1.5倍進(jìn)行擴(kuò)容,并將原數(shù)組的數(shù)據(jù)復(fù)制到擴(kuò)容后的數(shù)組/** 建議開(kāi)發(fā)中確定長(zhǎng)度后使用帶參構(gòu)造器,盡量避免擴(kuò)容來(lái)提高效率**/ ArrayList list = new ArrayList();list.add(123); list.add(456); //按照索引添加 list.add(1,"AA");System.out.println(list+"長(zhǎng)度是:"+list.size());//[123, 456, AA]長(zhǎng)度是:3List list1 = Arrays.asList(1, 2, 3); list.addAll(list1);//添加集合中的元素 System.out.println(list.size());//按照索引獲取 System.out.println(list.get(1)); //返回此元素首次+末次出現(xiàn)的索引,不存在返回-1, System.out.println(list.indexOf(123)+" "+list.lastIndexOf(123)); //指定索引位置元素值 list.set(1,"tylt"); //截取一定索引區(qū)間的元素[左閉右開(kāi)) System.out.println(list.subList(1, 3));

    2.2. Set

    無(wú)序、不可重復(fù) (HashSet、LinkedHashSet、TreeSet)

  • HashSet:Set接口的主要實(shí)現(xiàn)類(lèi),線程不安全,可以存儲(chǔ)null,底層是數(shù)組+鏈表結(jié)構(gòu),其實(shí)是個(gè)HashMap

  • LinkedHashSet:HashSet的子類(lèi),可按照添加的順序進(jìn)行遍歷(非有序)因?yàn)槎嗔穗p向鏈表進(jìn)行指向,效率高于HashSet

  • TreeSet:底層使用紅黑樹(shù)結(jié)構(gòu),可以按照添加的對(duì)象按執(zhí)行屬性排序,要求數(shù)據(jù)必須是同一類(lèi)型的,例如String Integer

  • 無(wú)序性不等同于隨機(jī)性,無(wú)序性在添加時(shí)不是按照數(shù)組索引的方式添加(線性探測(cè)進(jìn)入),而是按照數(shù)據(jù)的Hash值進(jìn)行位置確定,遍歷時(shí)依舊按照原加入順序輸出

    不可重復(fù)性就是添加的元素按照equals方法比較,即相同元素唯一

    3. Map接口

    雙列數(shù)據(jù),具有映射關(guān)系key-value(鍵值對(duì))

    • Map中的key是無(wú)序的、不可重復(fù)的,使用Set存儲(chǔ)所有key;value也是無(wú)序的、不可重復(fù)的,使用Collection存儲(chǔ)所有的value
    • 一個(gè)鍵值對(duì)構(gòu)成了一個(gè)Entry對(duì)象,是無(wú)序的不可重復(fù)的,使用Set存儲(chǔ),所以自定義的類(lèi)要重寫(xiě)equals和hashcode方法
  • HashMap:作為Map接口的主要實(shí)現(xiàn)類(lèi),線程不安全的,效率高,可存儲(chǔ)null的key和value,底層是數(shù)組+鏈表+紅黑樹(shù)

  • LinkedHashMap:HashMap的子類(lèi),雙向鏈表結(jié)構(gòu),可以按照順序?qū)崿F(xiàn)頻繁遍歷操作,效率高

  • TreeMap:存儲(chǔ)有序鍵值對(duì)數(shù)據(jù),可實(shí)現(xiàn)按照key排序,所以要求key必須由同一類(lèi)創(chuàng)建,底層使用紅黑樹(shù)

  • Hashtable:古老的實(shí)現(xiàn)類(lèi),線程安全

  • Properties:Hashtable子類(lèi),常用來(lái)處理配置文件,key,value都是String類(lèi)型

  • HashMap map = new HashMap(); //Insert map.put("A", 1); map.put("B", 1); map.put("C", 2); map.put("D", 3);//Update map.put("C", 10);HashMap map1 = new HashMap(); map.put("E", 4); map.put("F", 5); map.putAll(map1);System.out.println(map);//{A=1, B=1, C=10, D=3, E=4, F=5}//是否包含指定的key boolean containsKey = map.containsKey("A"); //是否包含指定的value boolean containsValue = map.containsValue(10);//Map的遍歷 //遍歷所有的key集合 Set keySet = map.keySet(); Iterator iterator = keySet.iterator(); while (iterator.hasNext()) {Object key = iterator.next();System.out.println(key+"--"+map.get(key)); }//遍歷所有的value集合 Collection values = map.values(); for (Object obj : values) {System.out.println(obj); } //遍歷匹配好的key-value Set entrySet = map.entrySet(); for (Object obj : entrySet) {System.out.println(obj);//強(qiáng)轉(zhuǎn)后分別獲取Map.Entry entry = (Map.Entry) obj;System.out.println(entry.getKey()+"---->"+entry.getValue()); }

    3.1. 源碼分析

    • HashMap 底層實(shí)現(xiàn)原理
    //底層創(chuàng)建長(zhǎng)度為16的Entry[] table數(shù)組 HashMap map = new HashMap(); //調(diào)用key所在類(lèi)的hashCode方法計(jì)算哈希值,經(jīng)過(guò)某種算法得到Entry數(shù)組中的存放位置, //若此位置為空則存放,不為空則進(jìn)行哈希值比較,若不相同則添加成功,若相同的話則調(diào)用key所在類(lèi)的equals方法比較 //若equals返回false則添加成功,返回true則進(jìn)行value替換 map.put("key","value"); /** JDK 8.0 中底層數(shù)組是Node[],在首次調(diào)用put時(shí)才會(huì)創(chuàng)建長(zhǎng)度為16的數(shù)組* 當(dāng)數(shù)組的某一個(gè)索引位置上的元素以鏈表形式存在的個(gè)數(shù) >8 且當(dāng)前數(shù)組長(zhǎng)度 >64時(shí),此索引位置上的所有數(shù)據(jù)改為紅黑樹(shù)存儲(chǔ)**/
    • Properties 配置文件的理解
    FileInputStream fileInputStream = null;try {Properties properties = new Properties();fileInputStream = new FileInputStream("test.properties");properties.load(fileInputStream);String name = properties.getProperty("name");String password = properties.getProperty("password");System.out.println("名稱:" + name + "---密碼:" + password);} catch (Exception e) {e.printStackTrace();} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}

    4. Collections工具類(lèi)

    操作Set、List、Map的靜態(tài)工具類(lèi)

    總結(jié)

    以上是生活随笔為你收集整理的集合(Collections)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。