Archive

Posts Tagged ‘Basic Java’

Java ArrayList Example Code for Using ArrayList in Java

June 17th, 2011
Comments Off

Content about : Java ArrayList
Advertisements


This Source code example for Create ArrayList Object and how to manage data in ArrayList and how to get Data from array List

Example code for Java ArrayList
package example.util;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample {
public void arrayListExample(){
//Create ArrayList Object
ArrayList<String> arrayList=new ArrayList<String>();
//Store and Remove data from ArrayList
arrayList.add(”value1″);
arrayList.add(”value2″);
arrayList.add(”value3″);
String value3=arrayList.remove(2);
System.out.println(”– Example for Get Value from ArrayList –”);
System.out.println(”Get ArrayList value: “+arrayList.get(0));
System.out.println(”Remove ArrayList value: “+value3);
System.out.println(”Contains ArrayList: “+arrayList.contains(”value1″));
System.out.println(”Contains ArrayList: “+arrayList.contains(”value3″));

System.out.println(”– Example for Iterate list from ArrayList –”);
Iterator<String> it=arrayList.iterator();
while(it.hasNext()){
String value=it.next();
System.out.println(”List Iterated  Value: “+value);
}
System.out.println(”– Example for Loop from ArrayList –”);
for (String temp:arrayList){
System.out.println(”For value ArrayList “+temp);
}

}
public static void main (String args[]){
ArrayListExample arryList=new ArrayListExample();
arryList.arrayListExample();
}
}

Running Result
– Example for Get Value from ArrayList –
Get ArrayList value: value1
Remove ArrayList value: value3
Contains ArrayList: true
Contains ArrayList: false
– Example for Iterate list from ArrayList –
List Iterated  Value: value1
List Iterated  Value: value2
– Example for Loop from ArrayList –
For value ArrayList value1
For value ArrayList value2

Note : index of arrayList start from 0

Development ,

Java Hashtable Example Code for using Hashtable in Java util

June 15th, 2011
Comments Off

Content about : Java Hashtable Example Code
Advertisements


This Source Code Example for : How to Create Hashtable Object , How to Store data to Hashtable, How to Remove data from Hashtable, How to get Dta from Hashtable and How to Iterate data from Hashtable with key and value

Example Source Code
package example.util;
import java.util.*;
public class HashtableExample {
public void hashtableExample(){
//Create Hashtable Object
Hashtable<String,String> hashtable=new Hashtable<String,String>();
//Store and Remove data from Hashtable
hashtable.put(”key1″, “value1″);
hashtable.put(”key2″, “value2″);
hashtable.put(”key3″, “value3″);
String key3=hashtable.remove(”key3″);

System.out.println(”– Example for Get Value from Hashtable –”);
System.out.println(”Get Hashtable value: “+hashtable.get(”key1″));
System.out.println(”Remove Hashtable value: “+key3);
System.out.println(”Contains Key Hashtable: “+hashtable.containsKey(”key1″));
System.out.println(”Contains Key Hashtable: “+hashtable.containsKey(”key3″));
System.out.println(”Contains Value Hashtable: “+hashtable.containsValue(”value1″));
System.out.println(”Contains Value Hashtable: “+hashtable.containsValue(”value3″));

System.out.println(”– Example for Iterate list from Hashtable –”);
Iterator<String> it=hashtable.keySet().iterator();
while(it.hasNext()){
String key=it.next();
String value=hashtable.get(key);
System.out.println(”By Key :Key : “+key+”   Value: “+value);
}

it=hashtable.values().iterator();
while(it.hasNext()){
System.out.println(”Value “+it.next());
}
}
public static void main (String args[]){
HashtableExample hashtable=new HashtableExample();
hashtable.hashtableExample();
}
}

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

Development ,

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 , , ,