Awesome q2a theme



0 votes
10 views
package com.mkyong.examples;

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

public class TestMap {

   public static void main(String[] args) {

       Map<String, Integer> fruits = new HashMap<>();
       fruits.put("apple", 1);
       fruits.put("orange", 2);
       fruits.put("banana", 3);
       fruits.put("watermelon", null);

       System.out.println("1. Is key 'apple' exists?");
       if (fruits.containsKey("apple")) {
           //key exists
           System.out.println("yes! - " + fruits.get("apple"));
       } else {
           //key does not exists
           System.out.println("no!");
       }

       System.out.println("\n2. Is key 'watermelon' exists?");
       if (fruits.containsKey("watermelon")) {
           System.out.println("yes! - " + fruits.get("watermelon"));
       } else {
           System.out.println("no!");
       }

   }

}

Output

1. Is key 'apple' exists?
yes! - 1

2. Is key 'watermelon' exists?
yes! - null
asked Sep 11 in Java Interview Questions by rajesh | 10 views



Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Related Questions



Welcome to Knowledge Boostr, where you can ask questions and receive answers from other members of the community.
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers


23,444 questions
40,127 answers
12 comments
23,597 users