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: Is it possible to add only String and Integer to ArrayList(Must be generic) by using JAVA GENERICS

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it possible to add only String and Integer to ArrayList(Must be generic) by using JAVA GENERICS

    Hi

    any one please help to solve title problem....

    Problem like:

    i am interested to add integer objects and String objects into any collection object ..... if it is possible please provide source code..

    while iterating the collection object i am not interested to do any type cast in java


    Thanks in advance...


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Is it possible to add only String and Integer to ArrayList(Must be generic) by using JAVA GENERICS

    Not directly. But you can implement a new class (or interface) that can wrap-up either a String or an Integer and then store this class in your collection.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Is it possible to add only String and Integer to ArrayList(Must be generic) by using JAVA GENERICS

    If two different types are in the arraylist, how will the code know which type is returned by the get() method?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Is it possible to add only String and Integer to ArrayList(Must be generic) by using JAVA GENERICS

    List<Object> list = new ArrayList<Object>();
    	list.add(new Integer(1));
    	list.add(new String("Tom"));
    	System.out.println(list);

    I really dont know why I'm writing this..kind of defeats the whole purpose of using generics in Java, which happens to be a compile time check to help the developer write a better code(which is, not to put an Integer in a list of String)..Probably you can tell us about what is the problem statement that you are trying to solve rather than trying to come up with a solution on your own and then asking for that solution ?
    Last edited by dextr; August 29th, 2014 at 10:29 AM.

Similar Threads

  1. Joining two different generic type of ArrayList.
    By Aika in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2013, 03:20 PM
  2. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  3. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  4. How to use Generics (1 generic anyway)
    By javapenguin in forum The Cafe
    Replies: 4
    Last Post: February 7th, 2011, 06:15 PM
  5. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM

Tags for this Thread