溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Java中利用entrySet方法獲取Map集合的元素

發(fā)布時(shí)間:2021-01-14 15:40:14 來(lái)源:億速云 閱讀:146 作者:Leah 欄目:編程語(yǔ)言

這篇文章給大家介紹怎么在Java中利用entrySet方法獲取Map集合的元素,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

具體內(nèi)容如下

/*---------------------------------
使用entrySet方法取出Map集合中的元素:
....該方法是將Map集合中key與value的關(guān)系存入到了Set集合中,這個(gè)關(guān)系的數(shù)據(jù)類型是Map.Entry
....entrySet方法返回值類型的具體寫(xiě)法為:Set< Map.Entry<KeyType , ValueType> >
----------------------------------*/
package pack04;

import java.util.*;

public class MapDemo {
  public static void main(String[] args) {

    Map<Integer, String> ma = new HashMap<Integer, String>();

    //給集合中存入元素
    ma.put(1, "abc01");  //這里將基本數(shù)據(jù)類型自動(dòng)裝箱
    ma.put(2, "abc02");
    ma.put(3, "abc03");
    ma.put(4, "abc04");
    
    //將Map集合當(dāng)中的映射關(guān)系取出,存入到Set集合當(dāng)中
    Set< Map.Entry<Integer, String> > entryset = ma.entrySet();
    
    //取迭代器
    Iterator< Map.Entry<Integer, String> > it = entryset.iterator();
    
    //獲取Map集合中的元素
    while( it.hasNext() ) {
      Map.Entry<Integer, String> en = it.next();
      Integer maKey = en.getKey();
      String maValue = en.getValue();
      
      System.out.println( maKey + " : " + maValue );
    }
    
  }
}

關(guān)于怎么在Java中利用entrySet方法獲取Map集合的元素就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI