Java TreeMap values() Method



Description

The Java TreeMap 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

Getting Collection of Values of a TreeMap of Integer,Integer Pair Example

The following example shows the usage of Java TreeMap values() method to get the collection view of the values present in the map. We've created a TreeMap object of Integer,Integer pairs. Then few entries are added, and using values() we're printing a values in ascending order.

package com.tutorialspoint;

import java.util.String;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();
      SortedMap<Integer, Integer> treemapincl = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);      

      System.out.println("Getting collection from the map");
      Collection coll = treemap.values();
      System.out.println("Value of the collection: "+coll);      
   }    
}

Output

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

Getting collection from the map
Value of the collection: [1, 2, 3, 4, 5]

Getting Collection of Values of a TreeMap of Integer,String Pair Example

The following example shows the usage of Java TreeMap values() method to get the collection view of the values present in the map. We've created a TreeMap object of Integer,String pairs. Then few entries are added, and using values() we're printing a values in ascending order.

package com.tutorialspoint;

import java.util.String;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<>();
      SortedMap<Integer, String> treemapincl = new TreeMap<>();

      // 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);      
   }    
}

Output

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]

Getting Collection of Values of a TreeMap of Integer,Object Pair Example

The following example shows the usage of Java TreeMap values() method to get the collection view of the values present in the map. We've created a TreeMap object of Integer,Student pairs. Then few entries are added, and using values() we're printing a values in ascending order.

package com.tutorialspoint;

import java.util.String;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, Student> treemap = new TreeMap<>();
      SortedMap<Integer, Student> treemapincl = new TreeMap<>();

      // 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);      
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

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]
java_util_treemap.htm
Advertisements