Java集合綜述

來源:互聯網
上載者:User

標籤:

Java集合圖,虛線框為介面,實線框是具體的類

具體實作類別

基本使用

(1)List:

List基本操作ArrayList<String> arrayList = new ArrayList<String>();        arrayList.add("Tom");        arrayList.add("Jerry");        arrayList.add("Micky");        // 使用Iterator遍曆元素        Iterator<String> it = arrayList.iterator();        while (it.hasNext()) {            String str = it.next();            System.out.println(str);        }        // 在指定位置插入元素        arrayList.add(2, "Kate");        // 通過索引直接存取元素        for (int i = 0; i < arrayList.size(); i++) {            System.out.println(arrayList.get(i));        }        List<String> subList = new ArrayList<String>();        subList.add("Mike");        // addAll(Collection<? extends String> c)添加所給集合中的所有元素        arrayList.addAll(subList);        // 判斷是否包含某個元素        if (arrayList.contains("Mike")) {            System.out.println("Mike is include in the list");        }        LinkedList<String> linkedList = new LinkedList<String>();        linkedList.addAll(arrayList);        // 擷取指定元素        System.out.println(linkedList.get(4));        // 擷取第一個元素        System.out.println(linkedList.getFirst());        // 擷取最後一個元素        System.out.println(linkedList.getLast());        // 擷取並刪除第一個元素        System.out.println(linkedList.pollFirst());        // 擷取,但不移除第一個元素        System.out.println(linkedList.peekFirst());

(2)Map:

Map基本操作HashMap<String, Integer> map = new HashMap<String, Integer>();        // 向Map中添加元素        map.put("Tom", 26);        map.put("Jack", 18);        map.put("Micky", 17);        map.put("Kate", 15);        // 根據Key擷取Value        System.out.println("Jack is " + map.get("Jack") + " years old");        // 移除        map.remove("Micky");        // 遍曆Map        for (Entry<String, Integer> entry : map.entrySet()) {            System.out.println("name:" + entry.getKey() + " age:"                    + entry.getValue());        }        // Key相同的元素將被覆蓋        map.put("Jack", 19);        // 根據Key擷取Value        System.out.println("Jack is " + map.get("Jack") + " years old");        // 判斷是否包含某個Key        if (map.containsKey("Tom")) {            System.out.println(map.get("Tom"));        }        // 判斷是否包含某個Value        if (map.containsValue(26)) {            System.out.println("The map include the value 26");        }        // 判斷map是否為空白        if (!map.isEmpty()) {            // 擷取map大小            System.out.println("The map‘s size=" + map.size());        }        // 擷取Key的集合        for (String str : map.keySet()) {            System.out.println(str);        }        TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>();        treeMap.putAll(map);        // 輸出內容按照key值排序        for (Entry<String, Integer> entry : treeMap.entrySet()) {            System.out.println("name:" + entry.getKey() + " age:"                    + entry.getValue());            // name:Jack age:19            // name:Kate age:15            // name:Tom age:26        }        LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<String, Integer>();        // 向Map中添加元素        linkedHashMap.put("Tom", 26);        linkedHashMap.put("Jack", 18);        linkedHashMap.put("Micky", 17);        linkedHashMap.put("Kate", 15);        // 保持了插入的順序        for (Entry<String, Integer> entry : linkedHashMap.entrySet()) {            System.out.println("name:" + entry.getKey() + " age:"                    + entry.getValue());            // name:Tom age:26            // name:Jack age:18            // name:Micky age:17            // name:Kate age:15        }

(3)Set:

Set基礎操作           List<Integer> list = new ArrayList<Integer>();           list.add(3);           list.add(4);           HashSet<Integer> hashSet = new HashSet<Integer>();           hashSet.add(1);           hashSet.add(3);           hashSet.add(2);           hashSet.add(6);           // 重複元素將不能被添加           hashSet.add(3);           // 只要有元素被添加就返回true           if (hashSet.addAll(list)) {               System.out.println("Add success");           }           // 判斷是否存在某個集合           if (hashSet.containsAll(list)) {               System.out.println("The hashSet is contain 3 and 4");           }           Iterator<Integer> it = hashSet.iterator();           while (it.hasNext()) {               System.out.print(it.next() + " ");               // 1 2 3 4 6               // 看結果是被排序了,HashSet按照Hash函數排序,Integer值的HashCode就是其int值           }           // 換轉成數組           Object[] integers = hashSet.toArray();           for (int i = 0; i < integers.length; i++) {               System.out.print((Integer) integers[i]);           }           //移除元素           hashSet.remove(3);                      TreeSet<String> treeSet = new TreeSet<String>();           treeSet.add("C");           treeSet.add("A");           treeSet.add("D");           treeSet.add("B");           for (Iterator<String> strIt = treeSet.iterator(); strIt.hasNext();) {               System.out.print(strIt.next());               // ABCD 按照字母順序           }           LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();           linkedHashSet.add("C");           linkedHashSet.add("A");           linkedHashSet.add("D");           linkedHashSet.add("B");           for (Iterator<String> linkedIt = linkedHashSet.iterator(); linkedIt                   .hasNext();) {               System.out.print(linkedIt.next());               // CADB 按照插入順序           }  

Java集合綜述

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.