Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Generic programming example

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Post Generic programming example

    I hope to be useful for everyone.

    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>();
     
        }
    }

  2. The Following 2 Users Say Thank You to neo_2010 For This Useful Post:

    JavaPF (July 8th, 2009), Json (July 8th, 2009)


  3. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

    Here is another use of generics, I've written a VERY limited wrapper for a list just to demonstrate.

    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

  4. The Following User Says Thank You to Json For This Useful Post:

    JavaPF (July 8th, 2009)

  5. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #4
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: Generic programming example

    Thanks a lot for your comment again...I will do my best.