Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

Java Hashmap – containsKey(Object key) and containsValue(Object value) – Check if Key/Value Exists in Map

Crunchify Java Hashmap containsKey containsValue Tutorial

There are few concepts and technologies which comes by default with each and every programing language and Collection is one of them.

Java Collection is a very big field.

It comes with so many interfaces and operations.

In this tutorial we will go over Hashmap and two of it’s operation boolean containsKey(Object key) and boolean containsValue(Object value).

Let’s look at below Java code

package com.crunchify.tutorials;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author https://crunchify.com
 */

public class CrunchifyHashMapContainsKey {
        static Map<String, String> crunchifyComapnies = new HashMap<>();

        private static void checkIfValueExist(String value) {
                // Let's checkout if Value exist
                String result = crunchifyComapnies.containsValue(value) ? ("Value (" + value + ") exist")
                                : ("Value (" + value + ") doesn't exist");
                log(result);
        }

        private static void checkIfKeyExist(String key) {
                // Let's checkout if Key exist
                String result = crunchifyComapnies.containsKey(key) ? (crunchifyComapnies.get(key))
                                : ("Key (" + key + ") doesn't exist");
                log(result);
        }

        public static void main(String[] args) {

                crunchifyComapnies.put("Google", "Mountain View, CA");
                crunchifyComapnies.put("Yahoo", "Santa Clara, CA");
                crunchifyComapnies.put("Microsoft", "Redmond, WA");

                checkIfKeyExist("Google");
                checkIfKeyExist("Facebook");
                checkIfKeyExist("Twitter");
                checkIfKeyExist("Yahoo");

                System.out.println("\n");
                checkIfValueExist("Mountain View, CA");
                checkIfValueExist("San Jose, CA");
        }

        private static void log(Object object) {
                System.out.println(object);

        }
}

In above tutorial we are using Java’s short if else – ternary operator ?. It’s a shortened version of an if else command.

Here we are adding 3 key,value pairs to map crunchifyCompanies Hashmap. We have created two functions – checkIfKeyExist() and checkIfValueExist().

Those functions will check if key or value exist and calls a log() which prints result on Eclipse console.

Bonus tutorial: Create simple Threadsafe Cache using Hashmap

Output:

Here is an Eclipse console result:

Mountain View, CA
Key (Facebook) doesn't exist
Key (Twitter) doesn't exist
Santa Clara, CA

Value (Mountain View, CA) exist
Value (San Jose, CA) doesn't exist

The post Java Hashmap – containsKey(Object key) and containsValue(Object value) – Check if Key/Value Exists in Map appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires