Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default how to use genirics in collection

    hai....
    how can i use vector to accept any type of values?
    means i want to use vector to hold any type of value(that is it may be one String,one float,one int,one double)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ashokreddy.narra View Post
    hai....
    how can i use vector to accept any type of values?
    means i want to use vector to hold any type of value(that is it may be one String,one float,one int,one double)
    You have to use the 'wildcard' notation:

    Java Code:
    Vector<?> vec= new Vector<?>();
    Vector vec can contain any type of object.

    kind regards,

    Jos

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    And also think really, really hard about whether you actually want to be doing this...and think whether you could write a class that would better represent the data going into the Vector, and make it a Vector<MyClass> instead.

    Mixing datatypes in a Collection is usually a recipe for bugs.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    And also think really, really hard about whether you actually want to be doing this...and think whether you could write a class that would better represent the data going into the Vector, and make it a Vector<MyClass> instead.

    Mixing datatypes in a Collection is usually a recipe for bugs.
    I've used it once: a Map<String, ?> where the key represents a logical name and the value either is the class name to load or an instance of such a class in which case the object is 'cacheable', i.e. there's only one instance of it.

    Most of the time such collections end up as the recipient for ResultSets or similar; they're indeed a source of trouble ...

    kind regards,

    Jos

Similar Threads

  1. help for collection api
    By javastuden in forum New To Java
    Replies: 3
    Last Post: 02-24-2010, 11:52 AM
  2. collection framework
    By swati.jyoti in forum New To Java
    Replies: 8
    Last Post: 09-01-2009, 03:45 PM
  3. How to get NamingEnumeration from my collection?
    By hencre in forum Advanced Java
    Replies: 1
    Last Post: 10-18-2008, 08:01 PM
  4. persistent collection
    By nanaji in forum Advanced Java
    Replies: 4
    Last Post: 10-03-2008, 04:40 PM
  5. how to creat collection
    By STILET in forum Advanced Java
    Replies: 2
    Last Post: 07-29-2008, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •