JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to search a key in HashMap?


Description:

Below example shows how to find whether specified key exists or not. By using containsKey() method you can find out the key existance.


Code:
package com.java2novice.hashmap;

import java.util.HashMap;

public class MyHashMapKeySearch {
	
	public static void main(String a[]){
		HashMap<String, String> hm = new HashMap<String, String>();
		//add key-value pair to hashmap
		hm.put("first", "FIRST INSERTED");
		hm.put("second", "SECOND INSERTED");
		hm.put("third","THIRD INSERTED");
		System.out.println(hm);
		if(hm.containsKey("first")){
			System.out.println("The hashmap contains key first");
		} else {
			System.out.println("The hashmap does not contains key first");
		}
		if(hm.containsKey("fifth")){
			System.out.println("The hashmap contains key fifth");
		} else {
			System.out.println("The hashmap does not contains key fifth");
		}
	}
}

Output:
{second=SECOND INSERTED, third=THIRD INSERTED, first=FIRST INSERTED}
The hashmap contains key first
The hashmap does not contains key fifth
<< Previous Program | Next Program >>

List Of All HashMap Sample Programs:

  1. Basic HashMap Operations.
  2. How to iterate through HashMap?
  3. How to copy Map content to another HashMap?
  4. How to search a key in HashMap?
  5. How to search a value in HashMap?
  6. How to get all keys from HashMap?
  7. How to get entry set from HashMap?
  8. How to delete all elements from HashMap?
  9. How to eliminate duplicate user defined objects as a key from HashMap?
  10. How to find user defined objects as a key from HashMap?
  11. How to delete user defined objects as a key from HashMap?
Java2Novice - YouTube Channel
  1. Python Tutorial for Beginners
Knowledge Centre
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
Success consists of going from failure to failure without loss of enthusiasm.
-- Winston Churchill

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2019 by Nataraja Gootooru. All Rights Reserved.