Map接口存在于Java.util包中,它主要提供了三个方法:KeySet()entrySet()values()。这些方法分别用于检索Map的键、Map的键值对以及Map的值。由于这些方法是Map接口的一部分,所以我们可以在所有实现Map接口的类中使用这些方法,比如TreeMapHashMapLinkedHashMap

为了弄清楚它们之间的区别,让我们先从概念上单独看一下,然后再看一下实现,以弄清它们之间的主要区别。

方法1:keySet()方法

该方法用于返回该Map中包含的键的Set视图。这个集合是由Map支持的,所以Map的变化会反映在这个集合中,反之亦然。

语法:

Set keySet()
  • 参数: 此方法没有参数。
  • 返回: 该方法返回一个包含指定Map的键的集合。

实现:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;

// Class
class YiibaiDemo {

    // Main driver method
    public static void main(String[] args)
    {
        Map<Integer, String> map = new HashMap<>();

        // Adding the elements to the objects
        // Elements here are key-value pairs in the map

        // Custom input entries
        map.put(1, "Yiibai");
        map.put(2, "For");
        map.put(3, "Geeks");

        // Now, different ways of iteration are illustrated
        // to showcase keySet() method

        // Way 1
        // Iterating the keySet() using iterator

        // Creating an object of Integer type
        Iterator<Integer> itr = map.keySet().iterator();

        // Condition check where hasNext() method holds true
        // till there is single element remaining in the List
        while (itr.hasNext()) {

            // Print all the elements(key-value pairs)
            System.out.print(itr.next() + " ");
        }

        // New line
        System.out.println();

        // Way 2
        // Iterating the keySet()
        // using for loop
        for (Integer key : map.keySet()) {

            // Print all the key-value pairs
            System.out.print(key + " ");
        }

        // New line
        System.out.println();

        // Way 3
        // Iterating over the keySet() by
        // converting the map to the string
        // using the toString() method
        System.out.println(map.keySet().toString());
    }
}

运行结果:

1 2 3 
1 2 3 
[1, 2, 3]

方法2:values()方法

Java中HashMap类的java.util.HashMap.values()方法用于从地图的值中创建一个集合。它基本上返回HashMap中数值的一个集合视图。

语法:

hashMap.values()
  • 参数: 该方法不接受任何参数。
  • 返回值: 该方法用于返回一个包含Map所有值的集合视图。

实现: 下面是使用values()方法的Java程序

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;

// Class
// Main class
public class YiibaiDemo {

    // Main driver method
    public static void main(String[] args)
    {
        // Making map of Integer keys and String values
        Map<Integer, String> map = new HashMap<>();

        map.put(1, "Yiibai");
        map.put(2, "For");
        map.put(3, "Geeks");

        // values() method implemented by
        // demonstrating different ways of traversal

        // Way 1
        Iterator itr = map.values().iterator();

        // Condition check using hasNet() method which
        // holds true till there is single element remaining
        while (itr.hasNext()) {
            System.out.print(itr.next() + " ");
        }
        System.out.println();

        // Way 2
        for (String key : map.values()) {
            System.out.print(key + " ");
        }
        System.out.println();

        // Way 3
        System.out.println(map.values().toString());
    }
}

运行结果:

Yiibai For Geeks 
Yiibai For Geeks 
[Yiibai, For, Geeks]

现在最后落到结论上,下面来看看keySet()方法和values()方法的区别如下:

keySet()方法 values()方法
该方法返回Map中所有键的集合视图,即返回一个键的集合。 该方法返回Map中包含的所有值的集合视图。
如果Map发生任何变化,那么它们也可以在集合中观察到,因为集合是由Map支持的。 如果Map发生了任何变化,那么它们也可以在集合中观察到,因为集合是由Map支持的。
这个方法只在我们需要处理Map中的所有键的时候使用。 当只需要处理Map中的所有值时,就可以使用这个方法。

| ——- | ——- | ——- |

Java Map中keySet()和value()方法的区别

欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果。
转载请注明:文章转载自 有区别网 [http://www.vsdiffer.com]
本文标题:Java Map中keySet()和value()方法的区别
本文链接:https://www.vsdiffer.com/vs/difference-between-keyset-vs-value-method-in-java-map.html
免责声明:以上内容仅代表 个人看法、理解、学习笔记、总结和研究收藏。不保证其正确性,因使用而带来的风险与本站无关!如本网站内容冒犯了您的权益,请联系站长,邮箱: ,我们核实并会尽快处理。