Results 1 to 6 of 6
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Are Collection classes generic?

    I have always read that Collections classes like ArrayList and HashMap are generic. But I just read the following statement in a piece of code.

    Java Code:
    mSelectedItems = new ArrayList();  // Where we track the selected items
    Is this correct? Does this mean that any type can be added to this ArrayList?

    Shouldn't it have been mSelectedItems = new ArrayList<>() ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Are Collection classes generic?

    What version of java was that line of code written for? Older versions do not have generics
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Are Collection classes generic?

    Yes! However, generics were introduced in 2004 with Java 5. So either your code snippet was written before that or the author
    didn't use them (a very bad idea).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Are Collection classes generic?

    So either your code snippet was written before th
    What version of java was that line of code written for? Older versions do not have generics
    Don't know, the statement is from this Android developer guide. Android was unveiled in 2007 (source), so this guide can't possibly be older than that.

    or the author
    didn't use them (a very bad idea).
    So this means the Collections classes can be used without making them generic?
    __________________________________________________ _____

    I am really confused. I have done this:

    Java Code:
    final HashSet selectedItems = new HashSet();
    ...
    selectedItems.add(selectedItemIndexPosition); //*******
    ...
    sharedPreferencesEditor.putStringSet("selectedItemsNames", selectedItems);//*********
    where selectedItemIndexPosition is an int, so selectedItems is a Set of int variables; whereas the method putStringSet() method strictly takes a set of String variables. BUT no compile-time error there! :s
    Last edited by Zarah; 03-17-2016 at 06:34 AM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Are Collection classes generic?

    That's what you get when you use a raw Set; the compiler can't help you here, i.e. any type of Object can be stored in such a Set. Make your set a Set<Integer> and the compiler can tell you what is wrong in your code.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Are Collection classes generic?

    Quote Originally Posted by Zarah View Post
    So this means the Collections classes can be used without making them generic?
    Yes. It has to be that way for backward compatibility. New compilers should always be able to
    compile old code (and code before Java 5 had did not support generics). However, old compilers
    may not be able to compile newer code (because there may be features like generics or lambdas
    that the old compiler doesn't know about and can't parse). Similarly, newer JVM's should be able
    to execute older code. But older JVM's won't be able to execute newer code (generated by newer
    compilers). For example, a new bytecode might have been introduced that the older JVM knows nothing
    about. In the case of compiled code, version identifiers are embedded in the class files to ensure
    compatibility between the JVM and the JDK.

    Probably more info than you wanted.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Collection classes
    By chann in forum New To Java
    Replies: 6
    Last Post: 01-19-2012, 11:06 AM
  2. Serialization of Collection classes
    By me.anchit in forum Advanced Java
    Replies: 6
    Last Post: 09-23-2011, 12:57 PM
  3. Diff on Collection classes
    By mopurilakshmikarreddy in forum New To Java
    Replies: 1
    Last Post: 06-08-2011, 09:31 AM
  4. Generic Classes
    By marciojc in forum New To Java
    Replies: 0
    Last Post: 12-17-2010, 11:34 PM
  5. Generic collection type
    By dreuzel in forum Advanced Java
    Replies: 7
    Last Post: 12-23-2009, 06:05 PM

Posting Permissions

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