Home » Java programming language

Java EnumMap values() Method with Example

EnumMap Class values() method: Here, we are going to learn about the values() method of EnumMap Class with its syntax and example.
Submitted by Preeti Jain, on February 10, 2020

EnumMap Class values() method

  • values() method is available in java.util package.
  • values() method is used to get all the values in a Collection view of this enum map.
  • values() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • values() method does not throw an exception at the time of collecting values in a Collection.

Syntax:

    public Collection values();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Collection, it gets the Collection view of the values exists in this enum map.

Example:

// Java program to demonstrate the example 
// of Collection values() method of EnumMap

import java.util.*;

public class ValuesOfEnumMap {
    public enum Colors {
        RED,
        BLUE,
        PINK,
        YELLOW
    };
    public static void main(String[] args) {
        // We are creating an EnumMap object
        EnumMap < Colors, String > em =
            new EnumMap < Colors, String > (Colors.class);

        // By using put() method is to
        // add the linked values in an
        // EnumMap
        em.put(Colors.RED, "1");
        em.put(Colors.BLUE, "2");
        em.put(Colors.PINK, "3");
        em.put(Colors.YELLOW, "4");

        // Display EnumMap
        System.out.println("EnumMap: " + em);

        // By using values() method isto
        // represent the values element exists in an 
        // EnumMap to be viewed in a Collection object
        Collection val_ele = em.values();

        // Display values collection view of EnumMap
        System.out.println("em.values(): " + val_ele);
    }
}

Output

EnumMap: {RED=1, BLUE=2, PINK=3, YELLOW=4}
em.values(): [1, 2, 3, 4]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.