|
|
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
|
|
|
|
|
List Of All HashMap Sample Programs:- Basic HashMap Operations.
- How to iterate through HashMap?
- How to copy Map content to another HashMap?
- How to search a key in HashMap?
- How to search a value in HashMap?
- How to get all keys from HashMap?
- How to get entry set from HashMap?
- How to delete all elements from HashMap?
- How to eliminate duplicate user defined objects as a key from HashMap?
- How to find user defined objects as a key from HashMap?
- How to delete user defined objects as a key from HashMap?
|
|
|
Procedural Vs Object-oriented Programs
In procedural program, programming logic follows certain procedures and the instructions are executed one after another.
In OOP program, unit of program is object, which is nothing but combination of data and code.
In procedural program, data
is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the
security of the code.
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek
|