In this shot, we will discuss how to use the SortedMap.values()
method in Java.
The SortedMap.values()
method is present in the SortedMap
interface inside the java.util
package.
The SortedMap.values()
method is used to obtain the set of values present in the key-value pairs in the SortedMap
.
The SortedMap.values()
method doesn’t take any parameters.
The SortedMap.values()
method returns:
Set
: Set of values present in the key-value pairs in the SortedMap
.Let’s look at the code snippet.
import java.util.*;class Main{public static void main(String[] args){SortedMap<Integer, String> s = new TreeMap<Integer,String>();s.put(2,"Welcome");s.put(12,"to");s.put(31,"the");s.put(18,"world");s.put(25,"of");s.put(36,"java");System.out.println("SortedMap mappings are: "+ s);System.out.println("The values present in the SortedMap are " + s.values());}}
Main
class.main
function.SortedMap
consisting of Integer
type keys and String
type values.TreeMap.put()
method.SortedMap
.SortedMap.values()
method to obtain the set of values present in the key-value pairs in the SortedMap
and displayed it.In this way, we can use the SortedMap.values()
method in Java.