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
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Map<Integer, String> map = new HashMap<>();
map.put( 1 , "Geeks" );
map.put( 2 , "For" );
map.put( 3 , "Geeks" );
Iterator<String> itr = map.values().iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " " );
}
System.out.println();
for (String key : map.values()) {
System.out.println(key);
}
System.out.println();
System.out.println(map.values().toString());
}
}
|
Output
Geeks 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
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
Map<Integer, String> map = new HashMap<>();
map.put( 1 , "Geeks" );
map.put( 2 , "For" );
map.put( 3 , "Geeks" );
for (Map.Entry<Integer, String> entry :
map.entrySet()) {
Integer key = (Integer)entry.getKey();
String value = entry.getValue();
System.out.println(key + "=" + value);
}
Iterator<Map.Entry<Integer, String> > itr
= map.entrySet().iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
Stream.of(map.entrySet().toArray())
.forEach(System.out::println);
}
}
|
Output
1=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. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jan, 2022
Like Article
Save Article