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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java基础系列:集合总结(4)

發(fā)布時間:2025/3/20 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java基础系列:集合总结(4) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用 S e t s

Set完全就是一個 Collection,只是具有不同的行為(這是實例和多形性最理想的應用:用于表達不同的行為)。在這里,一個 Set 只允許每個對象存在一個實例(正如大家以后會看到的那樣,一個對象的“值”的構成是相當復雜的)
Set(接口) 添加到 Set 的每個元素都必須是獨一無二的;否則 Set 就不會添加重復的元素。添加到 Set 里的對象必須定義 equals(),從而建立對象的唯一性。 Set 擁有與 Collection 完全相同的接口。一個 Set 不能保證自己可按任何特定的順序維持自己的元素
HashSet 用于除非常小的以外的所有 Set。對象也必須定義 hashCode()
ArraySet 由一個數(shù)組后推得到的 Set。面向非常小的 Set 設計,特別是那些需要頻繁創(chuàng)建和刪除的。對于小
Set,與 HashSet 相比, ArraySet 創(chuàng)建和反復所需付出的代價都要小得多。但隨著 Set 的增大,它的性能也
會大打折扣。不需要 HashCode()
TreeSet 由一個“紅黑樹”后推得到的順序 Set(注釋⑦)。這樣一來,我們就可以從一個 Set 里提到一個
順序集合

public class Set1 {public static void testVisual(Set a) {Collection1.fill(a);Collection1.fill(a);Collection1.fill(a);Collection1.print(a); // No duplicates!// Add another set to this one:a.addAll(a);a.add("one");a.add("one");a.add("one");Collection1.print(a);// Look something up:System.out.println("a.contains(\"one\"): " + a.contains("one"));}public static void main(String[] args) {testVisual(new HashSet());testVisual(new TreeSet());} }

重復的值被添加到 Set,但在打印的時候,我們會發(fā)現(xiàn) Set 只接受每個值的一個實例。運行這個程序時,會注意到由 HashSet 維持的順序與 ArraySet 是不同的。這是由于它們采用了不同的方法來保存元素,以便它們以后的定位。 ArraySet 保持著它們的順序狀態(tài),而 HashSet 使用一個散列函數(shù),這是特別為快速檢索設計的)。

class MyType implements Comparable {private int i;public MyType(int n) {i = n;}public boolean equals(Object o) {return (o instanceof MyType) && (i == ((MyType) o).i);}public int hashCode() {return i;}public String toString() {return i + " ";}public int compareTo(Object o) {int i2 = ((MyType) o).i;return (i2 < i ? -1 : (i2 == i ? 0 : 1));} }public class Set2 {public static Set fill(Set a, int size) {for (int i = 0; i < size; i++)a.add(new MyType(i));return a;}public static Set fill(Set a) {return fill(a, 10);}public static void test(Set a) {fill(a);fill(a); // Try to add duplicatesfill(a);a.addAll(fill(new TreeSet()));System.out.println(a);}public static void main(String[] args) {test(new HashSet());test(new TreeSet());} }

但只有要把類置入一個 HashSet 的前提下,才有必要使用 hashCode()—— 這種情況是完全有可能的,因為通常應先選擇作為一個 Set 實現(xiàn)。

使用 M a p s

Map(接口) 維持“鍵-值”對應關系(對),以便通過一個鍵查找相應的值
HashMap 基于一個散列表實現(xiàn)(用它代替 Hashtable)。針對“鍵-值”對的插入和檢索,這種形式具有最穩(wěn)定的性能。可通過構建器對這一性能進行調(diào)整,以便設置散列表的“能力”和“裝載因子”
ArrayMap 由一個 ArrayList 后推得到的 Map。對反復的順序提供了精確的控制。面向非常小的 Map 設計,特別是那些需要經(jīng)常創(chuàng)建和刪除的。對于非常小的Map,創(chuàng)建和反復所付出的代價要比
HashMap 低得多。但在Map 變大以后,性能也會相應地大幅度降低
TreeMap 在一個“紅-黑”樹的基礎上實現(xiàn)。查看鍵或者“鍵-值”對時,它們會按固定的順序排列(取決于 Comparable 或 Comparator,稍后即會講到)。 TreeMap 最大的好處就是我們得到的是已排好序的結果。TreeMap 是含有 subMap()方法的唯一一種 Map,利用它可以返回樹的一部分

public class Map1 {public final static String[][] testData1 = {{ "Happy", "Cheerful disposition" },{ "Sleepy", "Prefers dark, quiet places" },{ "Grumpy", "Needs to work on attitude" },{ "Doc", "Fantasizes about advanced degree" },{ "Dopey", "'A' for effort" },{ "Sneezy", "Struggles with allergies" },{ "Bashful", "Needs self-esteem workshop" }, };public final static String[][] testData2 = {{ "Belligerent", "Disruptive influence" },{ "Lazy", "Motivational problems" },{ "Comatose", "Excellent behavior" } };public static Map fill(Map m, Object[][] o) {for (int i = 0; i < o.length; i++)m.put(o[i][0], o[i][1]);return m;}// Producing a Set of the keys:public static void printKeys(Map m) {System.out.print("Size = " + m.size() + ", ");System.out.print("Keys: ");Collection1.print(m.keySet());}// Producing a Collection of the values:public static void printValues(Map m) {System.out.print("Values: ");Collection1.print(m.values());}// Iterating through Map.Entry objects (pairs):public static void print(Map m) {Collection entries = m.entries();Iterator it = entries.iterator();while (it.hasNext()) {Map.Entry e = (Map.Entry) it.next();System.out.println("Key = " + e.getKey() + ", Value = "+ e.getValue());}}public static void test(Map m) {fill(m, testData1);// Map has 'Set' behavior for keys:fill(m, testData1);printKeys(m);printValues(m);print(m);String key = testData1[4][0];String value = testData1[4][1];System.out.println("m.containsKey(\"" + key + "\"): "+ m.containsKey(key));System.out.println("m.get(\"" + key + "\"): " + m.get(key));System.out.println("m.containsValue(\"" + value + "\"): "+ m.containsValue(value));Map m2 = fill(new TreeMap(), testData2);m.putAll(m2);printKeys(m);m.remove(testData2[0][0]);printKeys(m);m.clear();System.out.println("m.isEmpty(): " + m.isEmpty());fill(m, testData1);// Operations on the Set change the Map:m.keySet().removeAll(m.keySet());System.out.println("m.isEmpty(): " + m.isEmpty());}public static void main(String args[]) {System.out.println("Testing HashMap");test(new HashMap());System.out.println("Testing TreeMap");test(new TreeMap());} }

遍歷map實例

package com.test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Test { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("first", "linlin"); map.put("second", "好好學java"); map.put("third", "sihai"); map.put("first", "sihai2"); // 第一種:通過Map.keySet遍歷key和value System.out.println("===================通過Map.keySet遍歷key和value:==================="); for (String key : map.keySet()) { System.out.println("key= " + key + " and value= " + map.get(key)); } // 第二種:通過Map.entrySet使用iterator遍歷key和value System.out.println("===================通過Map.entrySet使用iterator遍歷key和value:==================="); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } // 第三種:通過Map.entrySet遍歷key和value System.out.println("===================通過Map.entrySet遍歷key和value:==================="); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } // 第四種:通過Map.values()遍歷所有的value,但是不能遍歷鍵key System.out.println("===================通過Map.values()遍歷所有的value:==================="); for (String v : map.values()) { System.out.println("value= " + v); } } }

輸出結果如下:

===================通過Map.keySet遍歷key和value:===================
key= third and value= sihai
key= first and value= sihai2
key= second and value= 好好學java
===================通過Map.entrySet使用iterator遍歷key和value:===================
key= third and value= sihai
key= first and value= sihai2
key= second and value= 好好學java
===================通過Map.entrySet遍歷key和value:===================
key= third and value= sihai
key= first and value= sihai2
key= second and value= 好好學java
===================通過Map.values()遍歷所有的value:===================
value= sihai
value= sihai2
value= 好好學java

總結

以上是生活随笔為你收集整理的java基础系列:集合总结(4)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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