本文為大家分享了使用entryset方法獲取map集合中元素的具體代碼,供大家參考,具體內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/*--------------------------------- 使用entryset方法取出map集合中的元素: ....該方法是將map集合中key與value的關系存入到了set集合中,這個關系的數據類型是map.entry ....entryset方法返回值類型的具體寫法為: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" ); //這里將基本數據類型自動裝箱 ma.put( 2 , "abc02" ); ma.put( 3 , "abc03" ); ma.put( 4 , "abc04" ); //將map集合當中的映射關系取出,存入到set集合當中 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 ); } } } |
注:希望與各位讀者相互交流,共同學習進步。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/EarthPioneer/p/9351996.html