Generic programming example
I hope to be useful for everyone.
Code Java:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
//Note : Java can't insert primitives into Tables,Lists or anything else expecting an Object
// So java provides wrapper types for this purpose (int ---> Integer).
public class GenericDemo {
/** Creates a new instance of GenericDemo */
public GenericDemo() {
}
public static void main(String args[]){
/*
*Here i'm create a arraylist hold integers only
*Simple if you want create generic type
*write_Container_Name <TypeName> varName = new write_Container_Name <TypeName>();
*varName.Member Function and so on.
*/
List<Integer> iArrayList = new ArrayList<Integer>();
iArrayList.add(100);
iArrayList.add(500);
//View Container content.
for(int i = 0 ; i < iArrayList.size();i++)
{
System.out.println(iArrayList.get(i));
}
/*
*Here i'm create a Maplist hold string for every item.
*/
Map<String,String> mplist = new HashMap<String,String>();
mplist.put("0","Author : Mohamed Saleh");
mplist.put("1","Subject : Demonstration about Generic Programming");
mplist.put("2","Audience : Every Programmer what to know how Generic Programming Looks Like.");
mplist.put("3" , "Subject : Generic Programming\nThis is (Genetics):Using DataStructure Object Depend On my Own Like (Any Object)\n");
mplist.put("4","Sample without Generic : //Declare a ArrayList\n ArrayList al = new ArrayList();\n al.add(\"Object\");\n al.add(500);");
mplist.put("5","Hint 01//This ArrayList al will manipulate with any object Type\nWhat if i want to manipulate with strings Only for specific purpose.\nSo i have to declate special type ArrayList for string in Same Time.");
mplist.put("6","Sample with Generic// ArrayList<String> myStrList = new ArrayList<String>()");
mplist.put("7","Hint 02//Then Using Current new Object Directly");
for(int x = 0 ; x < mplist.size() ; x++){
// Here i want to view data holds in MapList and there is a restriction (View By Key not Index Like Arrays or ArrayList)
// so i did a convert or casting to content of Key to Object
System.out.println(mplist.get((Object)Integer.toString(x)));
}
/*
*Here i'm create a Hashtable holds doubles only
*/
Hashtable<Double,Double> dblTable = new Hashtable<Double,Double>();
}
}
Re: Sample about Generic programming
A thing to note about generics is that they are actually only there at compile time. As soon as you have compiled the program they are gone since the JVM wont actually use them.
Generics are good though :D
Here is another use of generics, I've written a VERY limited wrapper for a list just to demonstrate.
Code Java:
import java.util.ArrayList;
import java.util.List;
public class GenericsExample1<T> {
private List<T> mylist = new ArrayList<T>();
public GenericsExample1() {
}
public T getFromMyList(final int index) {
return this.mylist.get(index);
}
public void addToMyList(final T e) {
this.mylist.add(e);
}
public static void main(String... arguments) {
final GenericsExample1<String> genericsExample1 = new GenericsExample1<String>();
genericsExample1.addToMyList("My string");
System.out.println(genericsExample1.getFromMyList(0));
}
}
Running this example should print out "My string".
// Json
Re: Generic programming example
Hey guys,
Nice post. +rep to both of you.
I've moved this thread to our Tips & Tutorials forum neo. I'm sure it will be very useful to lots of people :)
Re: Generic programming example
Thanks a lot for your comment again...I will do my best.