SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public class TestMap {
  5.     public static void main(String[] args) {
  6.         Map<String, Integer> fruits = new HashMap<>();
  7.         fruits.put("apple", 1);
  8.         fruits.put("orange", 2);
  9.         fruits.put("banana", 3);
  10.         fruits.put("watermelon", null);
  11.  
  12.         System.out.println("1. Is key 'apple' exists?");
  13.         if (fruits.containsKey("apple")) {
  14.             //key exists
  15.             System.out.println("yes! - " + fruits.get("apple"));
  16.         } else {
  17.             //key does not exists
  18.             System.out.println("no!");
  19.         }
  20.  
  21.         System.out.println("\n2. Is key 'watermelon' exists?");
  22.         if (fruits.containsKey("watermelon")) {
  23.             System.out.println("yes! - " + fruits.get("watermelon"));
  24.         } else {
  25.             System.out.println("no!");
  26.         }
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top