導航:首頁 > 編程語言 > java常用map

java常用map

發布時間:2022-03-08 14:43:07

java中map的常用遍歷方法

方法一 在for-each循環中使用entries來遍歷

這是最常見的並且在大多數情況下也是最可取的遍歷方式。在鍵值都需要時使用。

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {

System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

}

注意:for-each循環在java 5中被引入所以該方法只能應用於java 5或更高的版本中。如果你遍歷的是一個空的map對象,for-each循環將拋出NullPointerException,因此在遍歷前你總是應該檢查空引用。

方法二 在for-each循環中遍歷keys或values。

如果只需要map中的鍵或者值,你可以通過keySet或values來實現遍歷,而不是用entrySet。

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

//遍歷map中的鍵

for (Integer key : map.keySet()) {

System.out.println("Key = " + key);

}

//遍歷map中的值

for (Integer value : map.values()) {

System.out.println("Value = " + value);

}

該方法比entrySet遍歷在性能上稍好

方法三使用Iterator遍歷

使用泛型:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();

while (entries.hasNext()) {

Map.Entry<Integer, 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);

}

方法四、通過鍵找值遍歷(效率低)

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

for (Integer key : map.keySet()) {

Integer value = map.get(key);

System.out.println("Key = " + key + ", Value = " + value);

}

總結

如果僅需要鍵(keys)或值(values)使用方法二。

如果你使用的語言版本低於java 5,或是打算在遍歷時刪除entries,必須使用方法三。

否則使用方法一(鍵值都要)。

❷ java裡面的map是什麼

java為數據結構中的映射定義了一個介面java.util.Map
Map主要用於存儲健值對,根據鍵得到值,因此不允許鍵重復(重復了覆蓋了),但允許值重復。

❸ java中Map<,>是什麼意思

Map<?,?>是一對對的值,比如說裡面加入的是你的名字和身份證號碼時,可以通過你的名字找到省份證號碼,通過身份證可以找到你名字。

❹ java中map有幾種寫法,一般怎樣寫,有什麼區別嗎

Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> ma = new HashMap<>();
簡寫,就是前面指定泛型,後面就可以省略。
HashMap<String, Object> m = new HashMap<>();
HashMap<String, Object> mm = new HashMap<String, Object>();
不建議這么寫,一般是介面在左,實現類在右,實現依賴倒置原則。

❺ JAVA 里 的Map

1.User.java

publicclassUser{

privateStringuserName;//用戶名
privateStringpassword;//密碼
privateStringgender;
privateIntegerage;
privatedoublemoney;//錢

publicUser(StringuserName,Stringpassword,doublemoney){
super();
this.userName=userName;
this.password=password;
this.money=money;
}

publicUser(StringuserName,Stringpassword,Stringgender,Integerage,doublemoney){
super();
this.userName=userName;
this.password=password;
this.gender=gender;
this.age=age;
this.money=money;
}

publicStringgetGender(){
returngender;
}

publicvoidsetGender(Stringgender){
this.gender=gender;
}

publicIntegergetAge(){
returnage;
}

publicvoidsetAge(Integerage){
this.age=age;
}

publicStringgetUserName(){
returnuserName;
}

publicvoidsetUserName(StringuserName){
this.userName=userName;
}

publicStringgetPassword(){
returnpassword;
}

publicvoidsetPassword(Stringpassword){
this.password=password;
}

publicdoublegetMoney(){
returnmoney;
}

publicvoidsetMoney(doublemoney){
this.money=money;
}

publicStringtoString(){
return"Name:"+this.userName+"password:"+password+"gender:"+gender+"age:"+age
+"money:"+this.money;
}

}
2.Bank.java

importjava.util.HashMap;
importjava.util.Map;
importjava.util.Scanner;

{
=1L;

publicPermissionException(StringerrMsg){
super(errMsg);

}

}

publicclassBank{

privatestaticMap<String,User>bankMap=newHashMap<String,User>();
privatestaticScannerinput=newScanner(System.in);

/**
*增加用戶
*
*@paramuser
*用戶對象
*/
(Useruser){
bankMap.put(user.getUserName(),user);
}

/**
*取出用戶
*
*@paramuserName
*用戶名
*@returnuserornull
*/
publicstaticUsergetUser(StringuserName){
if(null==userName){
userName="";
}
returnbankMap.get(userName);
}

/**
*login
*/
publicstaticvoidlogin(){

intinputTimes=0;
Useruser;
System.out.print("Account:");
StringuserName=input.next();
do{
System.out.print("Password:");
Stringpassword=input.next();

user=getUser(userName);

if(user!=null&&user.getPassword().equals(password)){
break;
}
inputTimes++;
}while(inputTimes<3);

if(inputTimes>=3){
deny();
}else{
takeMoney(user);
}

}

/**
*takemoney
*/
publicstaticvoidtakeMoney(Useruser){

doubletake=0;
doubleremain=user.getMoney();

do{
System.out.print("Takehowmuch?input:");
take=input.nextDouble();
if(take>remain){
System.out.println(" Remain:"+remain+" ");
}
}while(take>remain);

System.err.println(" success,take:"+take+",remainis:"+(remain-take));

user.setMoney(remain-take);

}

/**
*permissiondeny
*/
publicstaticvoiddeny(){
try{

thrownewPermissionException("密碼錯誤,請重新輸進");
}catch(Exceptione){
System.out.println("密碼錯誤,請重新輸進");
}
}

publicstaticvoidmain(String[]args){
Useruser=newUser("3306","3306","JavaMonkey",26,1000);
createNewAccount(user);
login();

}

}

❻ Java中,map分為哪些種類

您好,提問者:
Map:HashMap、TreeMap、Hashtable。
1、HashMap:線程不安全,鍵、值不允許為null。
2、Hashtable:線程安全,鍵、值允許為null。
3、TreeMap:線程不安全、鍵、值不允許為null,底層二叉樹。

❼ java中map有哪幾種實現方式,並且比較異同

Map是一種把鍵對象和值對象進行關聯的容器,而一個值對象又可以是一個Map,依次類推,這樣就可形成一個多級映射。對於鍵對象來說,像Set 一樣,一個Map容器中的鍵對象不允許重復,這是為了保持查找結果的一致性;如果有兩個鍵對象一樣,那你想得到那個鍵對象所對應的值對象時就有問題了,可 能你得到的並不是你想的那個值對象,結果會造成混亂,所以鍵的唯一性很重要,也是符合集合的性質的。當然在使用過程中,某個鍵所對應的值對象可能會發生變 化,這時會按照最後一次修改的值對象與鍵對應。對於值對象則沒有唯一性的要求。你可以將任意多個鍵都映射到一個值對象上,這不會發生任何問題(不過對你的 使用卻可能會造成不便,你不知道你得到的到底是那一個鍵所對應的值對象)。
Map有兩種比較常用的實現:HashMap和TreeMap,WeakMap。HashMap 也用到了哈希碼的演算法,以便快速查找一個鍵,TreeMap則是對鍵按序存放,因此它便有一些擴展的方法,比如firstKey(),lastKey() 等,你還可以從TreeMap中指定一個范圍以取得其子Map。鍵和值的關聯很簡單,用pub(Object key,Object value)方法即可將一個鍵與一個值對象相關聯。用get(Object key)可得到與此key對象所對應的值對象,WeakMap這個用於內存自動釋放的。

❽ java中map的常用遍歷方法有哪些

ava中map的常用遍歷的具體方法有:

一 、在for-each循環中使用entries來遍歷。這是最常見的並且在大多數情況下也是最可取的遍歷方式。在鍵值都需要時使用。

二、 在for-each循環中遍歷keys或values。如果只需要map中的鍵或者值,你可以通過keySet或values來實現遍歷,而不是用entrySet。

三、使用Iterator遍歷。

四、通過鍵找值遍歷(效率低)。

總結:如果僅需要鍵(keys)或值(values)使用方法二。如果你使用的語言版本低於java 5,或是打算在遍歷時刪除entries,必須使用方法三。否則使用方法一(鍵值都要)。

❾ java map 的用法

map是個介面

用都是用HasMap等實現Map介面的類

創建:Map<String,String> map = new HashMap<String,String>();
插入元素:map.put("1","a");
移除元素: map.remove("1");
清空: map.clear();

具體參照java API
java.uitl.HashMap

❿ java中map集合用法

Map map=new HashMap();//實例化map對象map.put("key","value");//存放值(值以鍵(key)-值(value)方式存放。)System.out.print(map.get("key").toString());//取值 根據鍵就可以取到值

閱讀全文

與java常用map相關的資料

熱點內容
優信二手車解壓後過戶 瀏覽:62
Windows常用c編譯器 瀏覽:778
關於改善國家網路安全的行政命令 瀏覽:833
安卓如何下載網易荒野pc服 瀏覽:654
javainetaddress 瀏覽:104
蘋果4s固件下載完了怎麼解壓 瀏覽:1003
命令zpa 瀏覽:286
python編譯器小程序 瀏覽:945
在app上看視頻怎麼光線調暗 瀏覽:540
可以中文解壓的解壓軟體 瀏覽:593
安卓卸載組件應用怎麼安裝 瀏覽:913
使用面向對象編程的方式 瀏覽:339
程序員項目經理的年終總結範文 瀏覽:929
內衣的加密設計用來幹嘛的 瀏覽:433
淮安數據加密 瀏覽:292
魔高一丈指標源碼 瀏覽:982
松下php研究所 瀏覽:168
c回調java 瀏覽:401
夢幻端游長安地圖互通源碼 瀏覽:746
電腦本地文件如何上傳伺服器 瀏覽:313