關于java中遍歷map具體有四種方式,請看下文詳解。

成都創(chuàng)新互聯(lián)長期為上千余家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為坪山企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設,坪山網(wǎng)站改版等技術服務。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
1、這是最常見的并且在大多數(shù)情況下也是最可取的遍歷方式,在鍵值都需要時使用。
MapInteger, Integer map = new?HashMapInteger, Integer();
for?(Map.EntryInteger, Integer entry : map.entrySet()) {
System.out.println("Key = "?+ entry.getKey() + ", Value = "?+ entry.getValue());
}
2、在for-each循環(huán)中遍歷keys或values。
如果只需要map中的鍵或者值,你可以通過keySet或values來實現(xiàn)遍歷,而不是用entrySet。
MapInteger, Integer map = new?HashMapInteger, Integer();
for?(Integer key : map.keySet()) {
System.out.println("Key = "?+ key);
}
for?(Integer value : map.values()) {
System.out.println("Value = "?+ value);
}
該方法比entrySet遍歷在性能上稍好(快了10%),而且代碼更加干凈。
3、使用Iterator遍歷
使用泛型:
MapInteger, Integer map = new?HashMapInteger, Integer();
IteratorMap.EntryInteger, Integer entries = map.entrySet().iterator();
while?(entries.hasNext()) {
Map.EntryInteger, Integer entry = entries.next();
System.out.println("Key = "?+ entry.getKey() + ", Value = "?+ entry.getValue());
}
不使用泛型:
Map map = new?HashMap();
Iterator entries = map.entrySet().iterator();
while?(entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = "?+ key + ", Value = "?+ value);
}
4、通過鍵找值遍歷(效率低)
MapInteger, Integer map = new?HashMapInteger, Integer();
for?(Integer key : map.keySet()) {
Integer value = map.get(key);
System.out.println("Key = "?+ key + ", Value = "?+ value);
}
假設Map中的鍵值對為1=11,2=22,3=33,現(xiàn)用方法1來遍歷Map代碼和調(diào)試結(jié)果如下:
擴展資料:
1、HashMap的重要參數(shù)
HashMap 的實例有兩個參數(shù)影響其性能:初始容量 和加載因子。容量是哈希表中桶的數(shù)量,初始容量只是哈希表在創(chuàng)建時的容量。
加載因子 是哈希表在其容量自動增加之前可以達到多滿的一種尺度。當哈希表中的條目數(shù)超出了加載因子與當前容量的乘積時,則要對該哈希表進行 rehash 操作(即重建內(nèi)部數(shù)據(jù)結(jié)構(gòu)),從而哈希表將具有大約兩倍的桶數(shù)。
在Java編程語言中,加載因子默認值為0.75,默認哈希表元為101。
2、HashMap的同步機制
注意,此實現(xiàn)不是同步的。 如果多個線程同時訪問一個哈希映射,而其中至少一個線程從結(jié)構(gòu)上修改了該映射,則它必須保持外部同步。
(結(jié)構(gòu)上的修改是指添加或刪除一個或多個映射關系的任何操作;以防止對映射進行意外的非同步訪問,如下:
Map m = Collections.synchronizedMap(new HashMap(...));
參考資料:百度百科-Hashmap
二維數(shù)組定義:數(shù)據(jù)類型[][] 數(shù)組名 = new 數(shù)據(jù)類型[二維數(shù)組行數(shù)][二維數(shù)組列數(shù)]
如:int[] array = new int[5][4];
二維數(shù)組的遍歷:需要使用兩個變量來分別遍歷行和列,具體遍歷方法就很多啦,可以使用while語句、do-while語句、for語句,也可以相互結(jié)合使用。
如:
int?i?=?0,?j?=?0;
for(int?i?=?0;?i??array.length;?i++){
for(int?j?=?0;?j??array[i].length;?j++){
System.out.println(array[i][j]?+?"、");
}
System.out.println("");
}
Java遍歷一個字符串的每一個字母
String str = "asdfghjkl";
方法1:
for(int i=0;istr.length();i++){
char ch = string.charAt(i);
}
方法2:
char[] c=s.toCharArray();
for(char cc:c){
...//cc直接用了
}
方法3:
for(int i=0;istr.length();i++){
String subStr = str.substring(i, i+1)
}
擴展資料:
Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進行復雜的編程 。
Java具有簡單性、面向?qū)ο蟆⒎植际健⒔研浴踩浴⑵脚_獨立與可移植性、多線程、動態(tài)性等特點 。Java可以編寫桌面應用程序、Web應用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應用程序等 。
參考資料:百度百科:JAVA
目錄結(jié)構(gòu)為樹型結(jié)構(gòu),用多線程不大好做,線程最多在前幾層進行分割,比如每個目錄下有兩個目錄,共5層,那么root目錄下就能啟用2個線程分別進行遍歷,所以第二層就啟動了2個線程進行遍歷,加上主線程共三個線程,雖然這樣做是可以做,但是要更具實際情況進行線程的規(guī)劃,否則容易線程過多導致cpu超負荷,或者假死,再提一點,遍歷目錄不建議用遞歸來寫,因為目錄較多容易棧溢出。
隨手寫了個,會有點bug就是關閉線程池的時候,還有就是有可能目錄太多進入拒絕策略,這個東西 可以考慮使用令牌桶算法,或者計數(shù)器算法來做。這里提供個簡單的例子。
public class TraverseUtil {
public static BlockingQueue blockingQueue = new LinkedBlockingQueue(100);
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100,100,10, TimeUnit.SECONDS,blockingQueue);
public static void traverseFolder2(String path) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
System.out.println("文件夾是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夾:" + file2.getAbsolutePath());
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
traverseFolder2(file2.getAbsolutePath());
}
});
} else {
System.out.println("文件:" + file2.getAbsolutePath());
}
}
}
} else {
System.out.println("文件不存在!");
}
}
public static void main(String[] args) throws InterruptedException {
traverseFolder2("C:\\Users\\a8932\\Desktop\\md");
}
}
這是一個表查找,查找表中與customer相等的數(shù)據(jù)。這個表有[customer,name,phone,....]等字段
這個可以使用遞歸來實現(xiàn),具體代碼如下:
import java.io.File;
public class Demo {
public static void main(String[] args) {
File file = new File("C:\\");// 指定
文件目錄
method(file);
}
public static void method(File file) {
File[] fs = file.listFiles();// 得到File數(shù)組
if(fs!=null) {// 判斷fs是否為null
for(File f : fs) {
if(f.isFile()) {// 如果是文件直接輸出
System.out.println(f.getName());
} else {
method(f);// 否則
遞歸調(diào)用
}
}
}
}
}
分享標題:JAVA程序遍歷代碼,編程遍歷是什么意思
轉(zhuǎn)載注明:http://chinadenli.net/article36/hcpipg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站制作、小程序開發(fā)、營銷型網(wǎng)站建設、軟件開發(fā)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)