Java LinkedHashMap entrySet()方法

java.util.LinkedHashMap.entrySet() 方法返回Map中所有Entry的Set集合。

1 语法

public Set<Map.Entry<K,V>> entrySet()

2 参数

3 返回值

返回Map中所有Entry的Set集合。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.LinkedHashMap.entrySet()方法的例子
 */
import java.util.*;

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

        // Instantiates a LinkedHashMap object
        Map < Integer, String > map = new LinkedHashMap < Integer, String > ();

        // By using put() method is to add
        // key-value pairs in a LinkedHashMap
        map.put(10, "C");
        map.put(20, "C++");
        map.put(50, "JAVA");
        map.put(40, "PHP");
        map.put(30, "SFDC");

        // Display LinkedHashMap
        System.out.println("LinkedHashMap: " + map);

        // By using entrySet() method is to
        // return the entry exists in this
        // LinkedHashMap to be viewed in a Set

        Set s = map.entrySet();

        // Display set view
        System.out.print("map.entrySet(): ");
        System.out.println(s);
    }
}

输出结果为:

LinkedHashMap: {10=C, 20=C++, 50=JAVA, 40=PHP, 30=SFDC}
map.entrySet(): [10=C, 20=C++, 50=JAVA, 40=PHP, 30=SFDC]

热门文章

优秀文章