Archive

Posts Tagged ‘java’

Java HashMap Example code for Using HashMap in Java

June 15th, 2011
Comments Off

Content about : Java HashMap
Advertisements


This Source Code  Example for: Create HashMap Object, Manage Data to HashMap eg Store Data , Remove  and Clear Data .Example for get Data from Hash With Key and Value and Example for  Iterate data from HashMap with keySet and Value

Example Source Code
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();

}

}

Running Result
– Example for Get Value from HashMap –
Get Hashmap value: value1
Remove Hashmap value: value3
Contains Key Hashmap: true
Contains Key Hashmap: false
Contains Value Hashmap: true
Contains Value Hashmap: false
– Example for Iterate list from Hash Map –
By Key :Key : key1   Value: value1
By Key :Key : key2   Value: value2
Value value1
Value value2

Note:  remove method can return value of key that removed

Development , , ,

Java String to int Example Code for Convert string to int with Java

June 13th, 2011
Comments Off

Content about : Java String to int
Advertisements


Example for Convert String to int with Java
package example;
public class StringToIntExample {
public void stringToInt(String input){
try {
//Convert with Integer.parseInt(input);
int intvalue=Integer.parseInt(input);
System.out.println(”int value:”+(intvalue+100));
//Convert with Integer.valueOf(input);
int intvalue2=Integer.valueOf(input);
System.out.println(”int value:”+(intvalue2+200));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
StringToIntExample stringToIntExample=new StringToIntExample();
stringToIntExample.stringToInt(”500″);
}
}

Running Result
int value:600
int value:700

**If String value sent to stringToInt method not NumberFormat it will throws Exception **
java.lang.NumberFormatException: For input string: “500q”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at example.StringToIntExample.stringToInt(StringToIntExample.java:7)
at example.StringToIntExample.main(StringToIntExample.java:18)

Development , , , ,