好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java使用entrySet方法获取Map集合中的元素

本文为大家分享了使用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

查看更多关于Java使用entrySet方法获取Map集合中的元素的详细内容...

  阅读:45次