Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

containsValue()

Example

Verify the presence of a value within a map.

import java.util.HashMap;
public class Main {
  public static void main(String[] args) {
    HashMap<String, String> capitalCities = new HashMap<String, String>();
    capitalCities.put("England", "London");
    capitalCities.put("Germany", "Berlin");
    capitalCities.put("Norway", "Oslo");
    capitalCities
.put("USA", "Washington DC");
    System.out.println(capitalCities.containsValue("London"));
    System.out.println(capitalCities.containsValue("Ottawa"));
  }                
}

Definition and Usage

The containsValue() method returns true if a map contains an entry with the specified value, and false otherwise.

Syntax

public boolean containsValue(Object value)

Parameter Values

Parameter

Description

key

Required: The value to be searched for.

Technical Details

Returns:

It returns true if an entry with the specified value exists in the map; otherwise, it returns false.