java.util.TreeMap.values() Method

Advertisements


Description

The values() method is used to return a Collection view of the values contained in this map. The collection's iterator returns the values in ascending order of the corresponding keys.

Declaration

Following is the declaration for java.util.TreeMap.values() method.

public Collection<V> values()

Parameters

  • NA

Return Value

The method call returns a collection view of the values contained in this map.

Exception

  • NA

Example

The following example shows the usage of java.util.TreeMap.values()

package com.tutorialspoint;

import java.util.*;

public class TreeMapDemo {
   public static void main(String[] args) {
      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
      SortedMap<Integer, String> treemapincl = new TreeMap<Integer, String>();
            
      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");      
      
      System.out.println("Getting collection from the map");
      Collection coll=treemap.values();
      System.out.println("Value of the collection: "+coll);      
   }    
}

Let us compile and run the above program, this will produce the following result.

Getting collection from the map
Value of the collection: [one, two, three, five, six]



Advertisements
Advertisements