Tutorial66.com
@Share Application Development Skill.



Tutorial is tutorial...66 is My Country Code Up to 10 Years EXP for Application Development Role in Thailand.
collapse

Tutorial Category



Topic: Hashmap Example in Java Example for create instance , put ,get and iterate  (Read 131 times)


My Development skill
Java JSP Servlet EJB
Spring Framework
Hibernate Framework
iReport Jasper Report
Apache CXF
PHP Codigniter
Oracle PLSQL
Unix Shell Script
Hashmap Example in Java Example for create instance , put ,get and iterate
Hashmap is Collection class in packate java.util
- You can put data to Hashmap with Key and Value
- You can get data from Hashmap with Key
- You can iterate all Key or Value from HashMap

Example Code for Hashmap
Code: [Select]
package example.util;
import java.util.*;
public class HashmapExample {
public void hashmapExample(){
//Create HashMap Object
HashMap<String,String> hashmap=new HashMap<String,String>();
//Store and Remove data from HashMap
hashmap.put("key1", "value1");
hashmap.put("key2", "value2");
hashmap.put("key3", "value3");
String key3=hashmap.remove("key3");

System.out.println("-- Example for Get Value from HashMap --");
System.out.println("Get Hashmap value: "+hashmap.get("key1"));
System.out.println("Remove Hashmap value: "+key3);
System.out.println("Contains Key Hashmap: "+hashmap.containsKey("key1"));
System.out.println("Contains Key Hashmap: "+hashmap.containsKey("key3"));
System.out.println("Contains Value Hashmap: "+hashmap.containsValue("value1"));
System.out.println("Contains Value Hashmap: "+hashmap.containsValue("value3"));

System.out.println("-- Example for Iterate list from Hash Map --");
Iterator<String> it=hashmap.keySet().iterator();
while(it.hasNext()){
String key=it.next();
String value=hashmap.get(key);
System.out.println("By Key :Key : "+key+"   Value: "+value);
}

it=hashmap.values().iterator();
while(it.hasNext()){
System.out.println("Value "+it.next());
}



}
public static void main (String args[]){
HashmapExample hashmap=new HashmapExample();
hashmap.hashmapExample();
}
}






SimplePortal 2.3.3 © 2008-2010, SimplePortal