詳解Java中l(wèi)ist,set,map的遍歷與增強(qiáng)for循環(huán)
詳解Java中l(wèi)ist,set,map的遍歷與增強(qiáng)for循環(huán)
Java集合類可分為三大塊,分別是從Collection接口延伸出的List、Set和以鍵值對(duì)形式作存儲(chǔ)的Map類型集合。
關(guān)于增強(qiáng)for循環(huán),需要注意的是,使用增強(qiáng)for循環(huán)無法訪問數(shù)組下標(biāo)值,對(duì)于集合的遍歷其內(nèi)部采用的也是Iterator的相關(guān)方法。如果只做簡單遍歷讀取,增強(qiáng)for循環(huán)確實(shí)減輕不少的代碼量。
集合概念:
1.作用:用于存放對(duì)象
2.相當(dāng)于一個(gè)容器,里面包含著一組對(duì)象,其中的每個(gè)對(duì)象作為集合的一個(gè)元素出現(xiàn)
3.java的容器有集合類和數(shù)組,不同之處是
區(qū)別及其常用實(shí)現(xiàn)類
List接口:
列表有序 元素可重復(fù)
實(shí)現(xiàn)類:ArrayList:動(dòng)態(tài)數(shù)組列表
LinkedList:雙向鏈表
Set接口:
集無序,元素不可重復(fù)
實(shí)現(xiàn)類:HashSet:散列集
TreeSet:樹集 內(nèi)部排序
Map接口:
以鍵值對(duì)的方式存儲(chǔ)數(shù)據(jù) 數(shù)據(jù)-鍵不允許重復(fù)
實(shí)現(xiàn)類:HashSet:散列集
TreeSet:樹集 內(nèi)部排序
JDK1.0出現(xiàn)的集合類都是線程安全的,但效率低
JDK1.2出現(xiàn)的集合類都不是線程安全的,但效率高
代碼示例如下:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class ListAndSet{
public static void main(String[] args) {
setTest();
listTest();
}
// 遍歷Set集合
private static void setTest() {
Set<string> set = new HashSet<string>();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
set.add("E");
//set集合遍歷方法1:使用iterator
Iterator<string> it = set.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
//set集合遍歷方法2:使用增強(qiáng)for循環(huán)。
for(String s: set){
System.out.println(s);
}
}
// 遍歷list集合
private static void listTest() {
List<string> list = new ArrayList<string>();
list.add("111");
list.add("222");
list.add("333");
list.add("444");
list.add("555");
// 遍歷方式1:使用iterator
Iterator<string> it = list.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
// 遍歷方法2:使用傳統(tǒng)for循環(huán)進(jìn)行遍歷。
for (int i = 0, size = list.size(); i < size; i++) {
String value = list.get(i);
System.out.println(value);
}
// 遍歷方法3:使用增強(qiáng)for循環(huán)進(jìn)行遍歷。
for (String value : list) {
System.out.println(value);
}
}
}
//關(guān)于Map類型集合的遍歷,keySet()與entrySet()方法
//增強(qiáng)For循環(huán)
public class Map{
public static void main(String[] args) {
// 創(chuàng)建一個(gè)HashMap對(duì)象,并加入了一些鍵值對(duì)。
Map<string, string=""> maps = new HashMap<string, string="">();
maps.put("111", "111");
maps.put("222", "222");
maps.put("333", "333");
maps.put("444", "444");
maps.put("555", "555");
// 傳統(tǒng)的遍歷map集合的方法1; keySet()
//traditionalMethod1(maps);
// 傳統(tǒng)的遍歷map集合的方法2; entrySet()
//traditionalMethod2(maps);
// 使用增強(qiáng)For循環(huán)來遍歷map集合方法1; keySet()
//strongForMethod1(maps);
// 使用增強(qiáng)For循環(huán)來遍歷map集合方法2; entrySet()
strongForMethod2(maps);
}
private static void strongForMethod2(Map<string, string=""> maps) {
Set<entry<string, string="">> set = maps.entrySet();
for (Entry<string, string=""> entry : set) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
private static void strongForMethod1(Map<string, string=""> maps) {
Set<string> set = maps.keySet();
for (String s : set) {
String key = s;
String value = maps.get(s);
System.out.println(key + " : " + value);
}
}
// 使用entrySet()方法,獲取maps集合中的每一個(gè)鍵值對(duì),
private static void traditionalMethod2(Map<string, string=""> maps) {
Set<map.entry<string, string="">> sets = maps.entrySet();
// 取得迭代器遍歷出對(duì)應(yīng)的值。
Iterator<entry<string, string="">> it = sets.iterator();
while (it.hasNext()) {
Map.Entry<string, string=""> entry = (Entry<string, string="">) it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
// 使用keySet()方法,獲取maps集合中的所有鍵,遍歷鍵取得所對(duì)應(yīng)的值。
private static void traditionalMethod1(Map<string, string=""> maps) {
Set<string> sets = maps.keySet();
// 取得迭代器遍歷出對(duì)應(yīng)的值。
Iterator<string> it = sets.iterator();
while (it.hasNext()) {
String key = it.next();
String value = maps.get(key);
System.out.println(key + " : " + value);
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Eclipse中創(chuàng)建Web項(xiàng)目最新方法(2023年)
在Java開發(fā)人員中,最常用的開發(fā)工具應(yīng)該就是Eclipse,下面這篇文章主要給大家介紹了關(guān)于Eclipse中創(chuàng)建Web項(xiàng)目2023年最新的方法,需要的朋友可以參考下2023-09-09
Mybatis一對(duì)多關(guān)聯(lián)關(guān)系映射實(shí)現(xiàn)過程解析
這篇文章主要介紹了Mybatis一對(duì)多關(guān)聯(lián)關(guān)系映射實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Springmvc基于fastjson實(shí)現(xiàn)導(dǎo)包及配置文件
這篇文章主要介紹了Springmvc基于fastjson實(shí)現(xiàn)導(dǎo)包及配置文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
詳談Map的key、value值的數(shù)據(jù)類型不能為基本類型的原因
這篇文章主要介紹了詳談Map的key、value值的數(shù)據(jù)類型不能為基本類型的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Netty分布式pipeline管道創(chuàng)建方法跟蹤解析
這篇文章主要為大家介紹了Netty分布式pipeline管道創(chuàng)建方法跟蹤解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Java 使用getClass().getResourceAsStream()方法獲取資源
這篇文章主要介紹了Java 使用getClass().getResourceAsStream()方法獲取資源的相關(guān)資料,這里主要講解哪種方式可以獲取到文件資源,需要的朋友可以參考下2017-07-07
詳解java CountDownLatch和CyclicBarrier在內(nèi)部實(shí)現(xiàn)和場景上的區(qū)別
這篇文章主要介紹了詳解java CountDownLatch和CyclicBarrier在內(nèi)部實(shí)現(xiàn)和場景上的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

