Difference Between value() vs entrySet() Method in Java Map
Last Updated :
27 Jan, 2022
Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use can these methods with all the classes implementing the map interface like TreeMap, HashMap, and LinkedHashMap.
Method 1: values() method
The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap.
Syntax:
Hash_Map.values()
Parameters: The method does not accept any parameters.
Return Value: The method is used to return a collection view containing all the values of the map.
Example:
Java
// Java program demonstrating use of values() method
// Importing all input output classes
import java.io.*;
// Importing HashMap, Iterator, Map and Stream classes
// from the java.util package
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a Map object
// Declaring object of String and integer type
Map<Integer, String> map = new HashMap<>();
// Now, adding the elements to the object created
// Elements here are key- \value pairs
// Custom input objects
map.put(1, "Geeks");
map.put(2, "For");
map.put(3, "Geeks");
// Showcasing different ways to illustrate
// values() method
// Way 1 - Using iterator
// Iterating over the object elements of the
// showcasing the values() method using iterator
// Creating an object of Integer type
Iterator<String> itr = map.values().iterator();
// Condition check which holds true till
// there is single elementusing hasNext() method
while (itr.hasNext()) {
// Traversing across the elements
// using next() method
// Printing the elements in the object
System.out.print(itr.next() + " ");
}
// New line
System.out.println();
// Way 2 - Using loops
// Iterating over the elements using for-each loop
// to showacase value() method
for (String key : map.values()) {
// Printing all the element in object
// key-value pairs
System.out.println(key);
}
// New line
System.out.println();
// Iterating over the values() method by
// converting the Map to the string
System.out.println(map.values().toString());
}
}
OutputGeeks For Geeks
Geeks
For
Geeks
[Geeks, For, Geeks]
Method 2: entrySet() method
The java.util.HashMap.entrySet() method in Java is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map, or we can create a new set and store the map elements into them.
Syntax:
hash_map.entrySet()
Parameters: The method does not take any parameters.
Return Value: The method returns a set having the same elements as the hash map.
Implementation:
Example
Java
// Java program demonstrating use of entrySet() method
// Importing Map,Stream, hashMap and Iterator classes
// from the java.util package
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Map class
// Declaring object of Integer and String type
Map<Integer, String> map = new HashMap<>();
// Now, adding the elements to the object
// Here elements are key-value pairs to map
// Custom input elements
map.put(1, "Geeks");
map.put(2, "For");
map.put(3, "Geeks");
// Now, proposing different cases in which we will
// be iterating over the elements using entrySet()
// Case 1
// Iterating the key value pairs
// using for each loop
for (Map.Entry<Integer, String> entry :
map.entrySet()) {
// Corresponding key
Integer key = (Integer)entry.getKey();
// Corresponding pair
String value = entry.getValue();
// Printing all the corresponding key-value
// pairs
System.out.println(key + "=" + value);
}
// Case 2
// Iterating the key-value pairs
// using iterator
Iterator<Map.Entry<Integer, String> > itr
= map.entrySet().iterator();
// Condition check using hasNext() method holding
// true till there is single entry remaining
while (itr.hasNext()) {
// Go on printing key-value pairs
System.out.println(itr.next());
}
// Case 3
// Iterating and printing the key-value pairs
// using Stream.of() method
// Printing alongside by using scope resolution
// operator
Stream.of(map.entrySet().toArray())
.forEach(System.out::println);
}
}
Output1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks
Now let's see the differences between values() Method and entrySet() Method
values() Method | entrySet() Method |
---|
This method returns the collection view of all the values contained in the map. | This method returns the Set view of all the mappings present in the map, ie it returns a set of key, value pairs. |
If any changes happen to the map, then they can be observed in the collection also, as the method collection is backed up by the map. | If any changes happen to the map, then they can be observed in the set also, as the set is backed up by the map. |
This method is used when we only need to deal with values present in the map. | This method is used when we need to deal with keys as well as values present in the map. |
Similar Reads
Difference Between keySet() vs value() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use ca
4 min read
Difference Between keySet() and entrySet() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use th
4 min read
Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos
4 min read
Difference between TreeMap and TreeSet in Java TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order. Some important features of the TreeSet are: In TreeSet duplicate values are not allowed because it implements the SortedSet interface.Objects in a TreeSet a
2 min read
Difference Between List and Set in Java The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements. Declaration: public abstr
2 min read
What is the difference between Map and WeakMap in JavaScript ? In this article, we will talk about the difference between Map and WeakMap which are introduced by ES6. Javascript object supports only one key object. For supporting multiple key objects, Then Map comes on this path. Map: A Map is an unordered list of key-value pairs where the key and the value can
4 min read
Difference Between EnumMap and EnumSet in Java EnumMap and EnumSet both are the classes defined inside the java collection. In this article, we will learn the differences between EnumMap and EnumSet. EnumMap is the specialized implementation of the Map interface and the EnumSet is the specialized implementation of the Set interface. There are so
3 min read
Map Values() Method in Java With Examples Map Values() method returns the Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. Syntax: map.values() Parameter: This method does not take any parameter. Return value: It returns a collect
2 min read
Difference Between LinkedList and LinkedHashSet in Java In this article you will learn difference between LinkedList and LinkedHashSet in java. Prerequisite: LinkedList : LinkedHashSet LinkedList class implements the List and Deque interface and extends from AbstractSequentialList class. LinkedList class uses doubly linked list to store the elements. It
3 min read
IdentityHashMap values() Method in Java The java.util.IdentityHashMap.values() method of IdentityHashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: Identity_Hash_Map.values() Parameters: The method does not accept any parameters. Retur
2 min read