豪仕知识网--知识就是力量!

微信
手机版
生活常识

Java集合类介绍_java集合类详解

作者 :姑苏慕容 2024-01-06 18:27:50 围观 : 评论

Java集合类介绍_java集合类详解

豪士君测试所用平台

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

以下就是我们整理的Java集合类介绍,一起来看看,希望能帮助到您。

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

数组是一种很常见的数据结构,开始接触编程的时候多数程序都和数组相关。刚开始接触Java时也是一直使用数组写一些程序,后来越来越觉得数组这东西没法满足需求了,这时一位“前辈”对我说了一句:不会用集合类就等于没学过Java。然后才知道有集合类。

java集合类分为:set、list、map、queue四大体系。其中set代表无序、不可重复的集合;list代表有序、可重复的集合。map代表具有映射关系的集合;queue代表队列集合。

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

Java集合类与数组的区别:Java的集合类的长度是动态的,数组则是固定长度的。Java集合类与数组的联系:使用相应的toArray()和Arrays.asList()方法可以回想转换。想想已经是3、4年前的事了,时间如白驹过隙啊。

什么时候数组会显得力不从心,没法满足需求,需要集合类呢?

1、不知道具体数据长度

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HTTP://WWW.hAoz.net███████████████████████████东方金报网

2、需要自动排序

3、存储键值对

当然,上面的情况不是绝对的,只是数组比较难满足。这时集合类(也可称为容器类)就显示了它强大的功能。

豪仕知识网http://www.haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

集合类的分类

上图中不包含Queue内容,部分Map的实现类未给出。

常见使用的有List、Set、Map及他们的实现类。

●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

List、Set、Map接口及各实现类的特性

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

接口特性实现类实现类特性成员要求List线性、有序的存储容器,可通过索引访问元素ArrayList数组实现。非同步。Vector类似ArrayList,同步。LinkedList双向链表。非同步。Map保存键值对成员HashMap基于哈希表的 Map 接口的实现,满足通用需求任意Object对象,如果修改了equals方法,需同时修改hashCode方法TreeMap默认根据自然顺序进行排序,或者根据创建映射时提供的 Comparator进行排序键成员要求实现caparable接口,或者使用Comparator构造TreeMap。键成员一般为同一类型。LinkedHashMap类似于HashMap,但迭代遍历时取得“键值对”的顺序是其插入顺序或者最近最少使用的次序与HashMap相同IdentityHashMap使用==取代equals()对“键值”进行比较的散列映射成员通过==判断是否相等WeakHashMap弱键映射,允许释放映射所指向的对象ConcurrentHashMap线性安全的MapSet成员不能重复HashSet为快速查找设计的Set元素必须定义hashCode()TreeSet保持次序的Set,底层为树结构元素必须实现Comparable接口LinkedHashSet内部使用链表维护元素的顺序(插入的次序)元素必须定义hashCode()

在满足要求的情况下,Map应尽量使用HashMap,Set应尽量使用HashSet。

◐◐◐◐●☛█▼▲豪仕知识网HT●☛█▼▲◐◐◐◐●☛█▼▲

集合类的基本使用

HTTP://WWW.haoz.net◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐豪仕知识网

List

◐◐◐◐●☛█▼▲豪仕知识网HT●☛█▼▲◐◐◐◐●☛█▼▲

List基本操作


 1 ArrayList arrayList = new ArrayList(); 2         arrayList.add("Tom"); 3         arrayList.add("Jerry"); 4         arrayList.add("Micky"); 5         // 使用Iterator遍历元素 6         Iterator it = arrayList.iterator(); 7         while (it.hasNext()) { 8             String str = it.next(); 9             System.out.println(str);10         }11         // 在指定位置插入元素12         arrayList.add(2, "Kate");13         // 通过索引直接访问元素14         for (int i = 0; i < arrayList.size(); i++) {15             System.out.println(arrayList.get(i));16         }17         List subList = new ArrayList();18         subList.add("Mike");19         // addAll(Collection<? extends String> c)添加所给集合中的所有元素20         arrayList.addAll(subList);21         // 判断是否包含某个元素22         if (arrayList.contains("Mike")) {23             System.out.println("Mike is include in the list");24         }25 26         LinkedList linkedList = new LinkedList();27         linkedList.addAll(arrayList);28         // 获取指定元素29         System.out.println(linkedList.get(4));30         // 获取第一个元素31         System.out.println(linkedList.getFirst());32         // 获取最后一个元素33         System.out.println(linkedList.getLast());34         // 获取并删除第一个元素35         System.out.println(linkedList.pollFirst());36         // 获取,但不移除第一个元素37         System.out.println(linkedList.peekFirst());

ArrayList和LinkedList的效率比较

ArrayList添加元素的效率

 1 ArrayList arrList = new ArrayList(); 2         long startTimeMillis, endTimeMillis; 3         startTimeMillis = System.currentTimeMillis(); 4         for (int i = 0; i < 10000; i++) { 5             arrList.add(0, "addString"); 6         } 7         endTimeMillis = System.currentTimeMillis(); 8         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 9                 + "ms");10 11         arrList.clear();12 13         startTimeMillis = System.currentTimeMillis();14         for (int i = 0; i < 20000; i++) {15             arrList.add(0, "addString");16         }17         endTimeMillis = System.currentTimeMillis();18         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)19                 + "ms");20 21         arrList.clear();22 23         startTimeMillis = System.currentTimeMillis();24         for (int i = 0; i < 40000; i++) {25             arrList.add(0, "addString");26         }27         endTimeMillis = System.currentTimeMillis();28         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)29                 + "ms");30 31         arrList.clear();32 33         startTimeMillis = System.currentTimeMillis();34         for (int i = 0; i < 80000; i++) {35             arrList.add(0, "addString");36         }37         endTimeMillis = System.currentTimeMillis();38         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)39                 + "ms");40 41         arrList.clear();42 43         startTimeMillis = System.currentTimeMillis();44         for (int i = 0; i < 160000; i++) {45             arrList.add(0, "addString");46         }47         endTimeMillis = System.currentTimeMillis();48         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)49                 + "ms");50 51         arrList.clear();52 53         startTimeMillis = System.currentTimeMillis();54         for (int i = 0; i < 320000; i++) {55             arrList.add(0, "addString");56         }57         endTimeMillis = System.currentTimeMillis();58         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)59                 + "ms");
●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

执行时间比较

执行次数(在0号位置插入)ArrayList所用时间(ms)LinkedList所用时间(ms)1000031020000141040000484168000019850160000790603200003171916执行次数(在尾部插入)ArrayList所用时间(ms)LinkedList所用时间(ms)10000002000015040000008000000160000015320000016循环输出次数(get(index)方法)ArrayList所用时间(ms)LinkedList所用时间(ms)10000932042000018879740000328273480000688133281600001594623133200002765太久了……

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识网HTtp://www.haoZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

因为ArrayList底层由数组实现,在0号位置插入时将移动list的所有元素,在末尾插入元素时不需要移动。LinkedList是双向链表,在任意位置插入元素所需时间均相同。所以在List中有较多插入和删除操作的情况下应使用LinkedList来提高效率,而有较多索引查询的时候使用ArrayList(使用增强型的for循环或Iterator遍历LinkedList效率将提高很多)。

豪仕知识网http://www.haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

Map

Map基本操作

 1 HashMap map = new HashMap(); 2         // 向Map中添加元素 3         map.put("Tom", 26); 4         map.put("Jack", 18); 5         map.put("Micky", 17); 6         map.put("Kate", 15); 7         // 根据Key获取Value 8         System.out.println("Jack is " + map.get("Jack") + " years old"); 9         // 移除10         map.remove("Micky");11         // 遍历Map12         for (Entry entry : map.entrySet()) {13             System.out.println("name:" + entry.getKey() + " age:"14                     + entry.getValue());15         }16         // Key相同的元素将被覆盖17         map.put("Jack", 19);18         // 根据Key获取Value19         System.out.println("Jack is " + map.get("Jack") + " years old");20         // 判断是否包含某个Key21         if (map.containsKey("Tom")) {22             System.out.println(map.get("Tom"));23         }24         // 判断是否包含某个Value25         if (map.containsValue(26)) {26             System.out.println("The map include the value 26");27         }28         // 判断map是否为空29         if (!map.isEmpty()) {30             // 获取map大小31             System.out.println("The map's size=" + map.size());32         }33         // 获取Key的集合34         for (String str : map.keySet()) {35             System.out.println(str);36         }37 38         TreeMap treeMap = new TreeMap();39         treeMap.putAll(map);40         // 输出内容按照key值排序41         for (Entry entry : treeMap.entrySet()) {42             System.out.println("name:" + entry.getKey() + " age:"43                     + entry.getValue());44             // name:Jack age:1945             // name:Kate age:1546             // name:Tom age:2647         }48 49         LinkedHashMap linkedHashMap = new LinkedHashMap();50         // 向Map中添加元素51         linkedHashMap.put("Tom", 26);52         linkedHashMap.put("Jack", 18);53         linkedHashMap.put("Micky", 17);54         linkedHashMap.put("Kate", 15);55         // 保持了插入的顺序56         for (Entry entry : linkedHashMap.entrySet()) {57             System.out.println("name:" + entry.getKey() + " age:"58                     + entry.getValue());59             // name:Tom age:2660             // name:Jack age:1861             // name:Micky age:1762             // name:Kate age:1563         }
◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识网HTtp://www.haoZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

Set

Set基本操作

 1 HashMap map = new HashMap(); 2         // 向Map中添加元素 3         map.put("Tom", 26); 4         map.put("Jack", 18); 5         map.put("Micky", 17); 6         map.put("Kate", 15); 7         // 根据Key获取Value 8         System.out.println("Jack is " + map.get("Jack") + " years old"); 9         // 移除10         map.remove("Micky");11         // 遍历Map12         for (Entry entry : map.entrySet()) {13             System.out.println("name:" + entry.getKey() + " age:"14                     + entry.getValue());15         }16         // Key相同的元素将被覆盖17         map.put("Jack", 19);18         // 根据Key获取Value19         System.out.println("Jack is " + map.get("Jack") + " years old");20         // 判断是否包含某个Key21         if (map.containsKey("Tom")) {22             System.out.println(map.get("Tom"));23         }24         // 判断是否包含某个Value25         if (map.containsValue(26)) {26             System.out.println("The map include the value 26");27         }28         // 判断map是否为空29         if (!map.isEmpty()) {30             // 获取map大小31             System.out.println("The map's size=" + map.size());32         }33         // 获取Key的集合34         for (String str : map.keySet()) {35             System.out.println(str);36         }37 38         TreeMap treeMap = new TreeMap();39         treeMap.putAll(map);40         // 输出内容按照key值排序41         for (Entry entry : treeMap.entrySet()) {42             System.out.println("name:" + entry.getKey() + " age:"43                     + entry.getValue());44             // name:Jack age:1945             // name:Kate age:1546             // name:Tom age:2647         }48 49         LinkedHashMap linkedHashMap = new LinkedHashMap();50         // 向Map中添加元素51         linkedHashMap.put("Tom", 26);52         linkedHashMap.put("Jack", 18);53         linkedHashMap.put("Micky", 17);54         linkedHashMap.put("Kate", 15);55         // 保持了插入的顺序56         for (Entry entry : linkedHashMap.entrySet()) {57             System.out.println("name:" + entry.getKey() + " age:"58                     + entry.getValue());59             // name:Tom age:2660             // name:Jack age:1861             // name:Micky age:1762             // name:Kate age:1563         }

本文没有对ArrayList及HashMap进行深入的分析,这两个类是集合类中最常用的类,将另开文章进行深入剖析。

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识网HTtp://www.haoZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

数组是一种很常见的数据结构,开始接触编程的时候多数程序都和数组相关。刚开始接触Java时也是一直使用数组写一些程序,后来越来越觉得数组这东西没法满足需求了,这时一位“前辈”对我说了一句:不会用集合类就等于没学过Java。然后才知道有集合类。

java集合类分为:set、list、map、queue四大体系。其中set代表无序、不可重复的集合;list代表有序、可重复的集合。map代表具有映射关系的集合;queue代表队列集合。

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

Java集合类与数组的区别:Java的集合类的长度是动态的,数组则是固定长度的。Java集合类与数组的联系:使用相应的toArray()和Arrays.asList()方法可以回想转换。想想已经是3、4年前的事了,时间如白驹过隙啊。

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

什么时候数组会显得力不从心,没法满足需求,需要集合类呢?

1、不知道具体数据长度

2、需要自动排序

●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

3、存储键值对

当然,上面的情况不是绝对的,只是数组比较难满足。这时集合类(也可称为容器类)就显示了它强大的功能。

◐◐◐◐●☛█▼▲豪仕知识网http://www.haOz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

集合类的分类

◐◐◐◐●☛█▼▲豪仕知识网HT●☛█▼▲◐◐◐◐●☛█▼▲

上图中不包含Queue内容,部分Map的实现类未给出。

●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

常见使用的有List、Set、Map及他们的实现类。

List、Set、Map接口及各实现类的特性

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

接口特性实现类实现类特性成员要求List线性、有序的存储容器,可通过索引访问元素ArrayList数组实现。非同步。Vector类似ArrayList,同步。LinkedList双向链表。非同步。Map保存键值对成员HashMap基于哈希表的 Map 接口的实现,满足通用需求任意Object对象,如果修改了equals方法,需同时修改hashCode方法TreeMap默认根据自然顺序进行排序,或者根据创建映射时提供的 Comparator进行排序键成员要求实现caparable接口,或者使用Comparator构造TreeMap。键成员一般为同一类型。LinkedHashMap类似于HashMap,但迭代遍历时取得“键值对”的顺序是其插入顺序或者最近最少使用的次序与HashMap相同IdentityHashMap使用==取代equals()对“键值”进行比较的散列映射成员通过==判断是否相等WeakHashMap弱键映射,允许释放映射所指向的对象ConcurrentHashMap线性安全的MapSet成员不能重复HashSet为快速查找设计的Set元素必须定义hashCode()TreeSet保持次序的Set,底层为树结构元素必须实现Comparable接口LinkedHashSet内部使用链表维护元素的顺序(插入的次序)元素必须定义hashCode()

在满足要求的情况下,Map应尽量使用HashMap,Set应尽量使用HashSet。

HTTP://WWW.haoz.net◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐豪仕知识网

集合类的基本使用

List

List基本操作


 1 ArrayList arrayList = new ArrayList(); 2         arrayList.add("Tom"); 3         arrayList.add("Jerry"); 4         arrayList.add("Micky"); 5         // 使用Iterator遍历元素 6         Iterator it = arrayList.iterator(); 7         while (it.hasNext()) { 8             String str = it.next(); 9             System.out.println(str);10         }11         // 在指定位置插入元素12         arrayList.add(2, "Kate");13         // 通过索引直接访问元素14         for (int i = 0; i < arrayList.size(); i++) {15             System.out.println(arrayList.get(i));16         }17         List subList = new ArrayList();18         subList.add("Mike");19         // addAll(Collection<? extends String> c)添加所给集合中的所有元素20         arrayList.addAll(subList);21         // 判断是否包含某个元素22         if (arrayList.contains("Mike")) {23             System.out.println("Mike is include in the list");24         }25 26         LinkedList linkedList = new LinkedList();27         linkedList.addAll(arrayList);28         // 获取指定元素29         System.out.println(linkedList.get(4));30         // 获取第一个元素31         System.out.println(linkedList.getFirst());32         // 获取最后一个元素33         System.out.println(linkedList.getLast());34         // 获取并删除第一个元素35         System.out.println(linkedList.pollFirst());36         // 获取,但不移除第一个元素37         System.out.println(linkedList.peekFirst());
◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HtTp://wWW.haoz.net豪仕知识网●●●●●●●●●●●●●●●●●●●●●●●●●●

ArrayList和LinkedList的效率比较

ArrayList添加元素的效率

 1 ArrayList arrList = new ArrayList(); 2         long startTimeMillis, endTimeMillis; 3         startTimeMillis = System.currentTimeMillis(); 4         for (int i = 0; i < 10000; i++) { 5             arrList.add(0, "addString"); 6         } 7         endTimeMillis = System.currentTimeMillis(); 8         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 9                 + "ms");10 11         arrList.clear();12 13         startTimeMillis = System.currentTimeMillis();14         for (int i = 0; i < 20000; i++) {15             arrList.add(0, "addString");16         }17         endTimeMillis = System.currentTimeMillis();18         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)19                 + "ms");20 21         arrList.clear();22 23         startTimeMillis = System.currentTimeMillis();24         for (int i = 0; i < 40000; i++) {25             arrList.add(0, "addString");26         }27         endTimeMillis = System.currentTimeMillis();28         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)29                 + "ms");30 31         arrList.clear();32 33         startTimeMillis = System.currentTimeMillis();34         for (int i = 0; i < 80000; i++) {35             arrList.add(0, "addString");36         }37         endTimeMillis = System.currentTimeMillis();38         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)39                 + "ms");40 41         arrList.clear();42 43         startTimeMillis = System.currentTimeMillis();44         for (int i = 0; i < 160000; i++) {45             arrList.add(0, "addString");46         }47         endTimeMillis = System.currentTimeMillis();48         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)49                 + "ms");50 51         arrList.clear();52 53         startTimeMillis = System.currentTimeMillis();54         for (int i = 0; i < 320000; i++) {55             arrList.add(0, "addString");56         }57         endTimeMillis = System.currentTimeMillis();58         System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis)59                 + "ms");
◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

执行时间比较

执行次数(在0号位置插入)ArrayList所用时间(ms)LinkedList所用时间(ms)1000031020000141040000484168000019850160000790603200003171916执行次数(在尾部插入)ArrayList所用时间(ms)LinkedList所用时间(ms)10000002000015040000008000000160000015320000016循环输出次数(get(index)方法)ArrayList所用时间(ms)LinkedList所用时间(ms)10000932042000018879740000328273480000688133281600001594623133200002765太久了……

因为ArrayList底层由数组实现,在0号位置插入时将移动list的所有元素,在末尾插入元素时不需要移动。LinkedList是双向链表,在任意位置插入元素所需时间均相同。所以在List中有较多插入和删除操作的情况下应使用LinkedList来提高效率,而有较多索引查询的时候使用ArrayList(使用增强型的for循环或Iterator遍历LinkedList效率将提高很多)。

HTTP://WWW.haoz.net豪仕知识网采集不好玩哦◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐撒旦法师打发斯蒂芬

Map

Map基本操作

 1 HashMap map = new HashMap(); 2         // 向Map中添加元素 3         map.put("Tom", 26); 4         map.put("Jack", 18); 5         map.put("Micky", 17); 6         map.put("Kate", 15); 7         // 根据Key获取Value 8         System.out.println("Jack is " + map.get("Jack") + " years old"); 9         // 移除10         map.remove("Micky");11         // 遍历Map12         for (Entry entry : map.entrySet()) {13             System.out.println("name:" + entry.getKey() + " age:"14                     + entry.getValue());15         }16         // Key相同的元素将被覆盖17         map.put("Jack", 19);18         // 根据Key获取Value19         System.out.println("Jack is " + map.get("Jack") + " years old");20         // 判断是否包含某个Key21         if (map.containsKey("Tom")) {22             System.out.println(map.get("Tom"));23         }24         // 判断是否包含某个Value25         if (map.containsValue(26)) {26             System.out.println("The map include the value 26");27         }28         // 判断map是否为空29         if (!map.isEmpty()) {30             // 获取map大小31             System.out.println("The map's size=" + map.size());32         }33         // 获取Key的集合34         for (String str : map.keySet()) {35             System.out.println(str);36         }37 38         TreeMap treeMap = new TreeMap();39         treeMap.putAll(map);40         // 输出内容按照key值排序41         for (Entry entry : treeMap.entrySet()) {42             System.out.println("name:" + entry.getKey() + " age:"43                     + entry.getValue());44             // name:Jack age:1945             // name:Kate age:1546             // name:Tom age:2647         }48 49         LinkedHashMap linkedHashMap = new LinkedHashMap();50         // 向Map中添加元素51         linkedHashMap.put("Tom", 26);52         linkedHashMap.put("Jack", 18);53         linkedHashMap.put("Micky", 17);54         linkedHashMap.put("Kate", 15);55         // 保持了插入的顺序56         for (Entry entry : linkedHashMap.entrySet()) {57             System.out.println("name:" + entry.getKey() + " age:"58                     + entry.getValue());59             // name:Tom age:2660             // name:Jack age:1861             // name:Micky age:1762             // name:Kate age:1563         }

Set

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

Set基本操作

 1 HashMap map = new HashMap(); 2         // 向Map中添加元素 3         map.put("Tom", 26); 4         map.put("Jack", 18); 5         map.put("Micky", 17); 6         map.put("Kate", 15); 7         // 根据Key获取Value 8         System.out.println("Jack is " + map.get("Jack") + " years old"); 9         // 移除10         map.remove("Micky");11         // 遍历Map12         for (Entry entry : map.entrySet()) {13             System.out.println("name:" + entry.getKey() + " age:"14                     + entry.getValue());15         }16         // Key相同的元素将被覆盖17         map.put("Jack", 19);18         // 根据Key获取Value19         System.out.println("Jack is " + map.get("Jack") + " years old");20         // 判断是否包含某个Key21         if (map.containsKey("Tom")) {22             System.out.println(map.get("Tom"));23         }24         // 判断是否包含某个Value25         if (map.containsValue(26)) {26             System.out.println("The map include the value 26");27         }28         // 判断map是否为空29         if (!map.isEmpty()) {30             // 获取map大小31             System.out.println("The map's size=" + map.size());32         }33         // 获取Key的集合34         for (String str : map.keySet()) {35             System.out.println(str);36         }37 38         TreeMap treeMap = new TreeMap();39         treeMap.putAll(map);40         // 输出内容按照key值排序41         for (Entry entry : treeMap.entrySet()) {42             System.out.println("name:" + entry.getKey() + " age:"43                     + entry.getValue());44             // name:Jack age:1945             // name:Kate age:1546             // name:Tom age:2647         }48 49         LinkedHashMap linkedHashMap = new LinkedHashMap();50         // 向Map中添加元素51         linkedHashMap.put("Tom", 26);52         linkedHashMap.put("Jack", 18);53         linkedHashMap.put("Micky", 17);54         linkedHashMap.put("Kate", 15);55         // 保持了插入的顺序56         for (Entry entry : linkedHashMap.entrySet()) {57             System.out.println("name:" + entry.getKey() + " age:"58                     + entry.getValue());59             // name:Tom age:2660             // name:Jack age:1861             // name:Micky age:1762             // name:Kate age:1563         }
●☛█▼▲豪仕知识网◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲

本文没有对ArrayList及HashMap进行深入的分析,这两个类是集合类中最常用的类,将另开文章进行深入剖析。

以上的相关游戏回复就是Java集合类介绍这方面的内容介绍,,字数约23602字,也希望大家能够和我在一起进行分享。豪仕知识网往后会继续推荐Java集合类介绍相关内容。

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识网HTtp://www.haoZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

相关文章