Total members 11893 |It is currently Tue Nov 05, 2024 5:22 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Display the content of hashMap, check if contains a key, print its size, remove its content and finally check it if empty
Code:

import java
.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class 
ArraysWork {

    public static 
void main(String[] arr) {

        
/* Create object of HashMap */

        
HashMap<StringStringhashMap = new HashMap<StringString>();

        
/* Add entries to hashMap (Keys,Values) */
        
hashMap.put("A""Apple");
        
hashMap.put("B""Book");
        
hashMap.put("C""Car");
        
hashMap.put("D""Dog");
        
hashMap.put("E""Elephant");
        
hashMap.put("F""Flower");

        
// Print the content of the hashMap
        
Set<Entry<String,String>> hashSet=hashMap.entrySet();
        for(
Entry entry:hashSet ) {

            
System.out.println("Key="+entry.getKey()+", Value="+entry.getValue());
        }
        
System.out.println("-----------------------------");
        
// Print the size of hashMap
         
System.out.println("HashMap size="+hashMap.size());

        
// Checking and searching
        
if(hashMap.containsKey("X")) {
             
System.out.println("HashMap has a key X");
        }else{
             
System.out.println("HashMap hasn't a key X");
        }

         
// Remove the content of the hashMap
         
hashMap.clear();

         
// Check if the hashMap empty
         
if(hashMap.isEmpty())
         {
             
System.out.println("The hashMap is empty");
         }


    }
}
 


The output is :
Code:
Key=D, Value=Dog
Key=E, Value=Elephant
Key=F, Value=Flower
Key=A, Value=Apple
Key=B, Value=Book
Key=C, Value=Car
-----------------------------
HashMap size=6
HashMap hasn't a key X
The hashMap is empty




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Print the content of hashMap object
 print to console using the cout object with different Color     -  
 Read file content to StringBuffer String object     -  
 how to show hashmap value in a textbox     -  
 HashMap class with generics     -  
 Help for Print using php     -  
 Print the ASCII Set     -  
 Get union of two arrays set and print it     -  
 print clock using JavaScript     -  
 Print all files in a directory     -  
 print element in 2d matrix     -  



Topic Tags

Java Collections






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com