JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: Basic HashMap Operations.


Description:

Below example shows basic HashMap functionalities like creating object, adding entries, getting values by passing key, checking is hashmap is empty or not, deleting an element and size of the HashMap.


Code:
package com.java2novice.hashmap;

import java.util.HashMap;

public class MyBasicHashMap {

	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);
		//getting value for the given key from hashmap
		System.out.println("Value of second: "+hm.get("second"));
		System.out.println("Is HashMap empty? "+hm.isEmpty());
		hm.remove("third");
		System.out.println(hm);
		System.out.println("Size of the HashMap: "+hm.size());
	}
}

Output:
{second=SECOND INSERTED, third=THIRD INSERTED, first=FIRST INSERTED}
Value of second: SECOND INSERTED
Is HashMap empty? false
{second=SECOND INSERTED, first=FIRST INSERTED}
Size of the HashMap: 2
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?
Knowledge Centre
Where can we use serialization?
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of an object is to be saved, objects need to be serilazed.
Famous Quotations
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein

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 © 2022 by Nataraja Gootooru. All Rights Reserved.