This discussion is archived
9 Replies Latest reply: Oct 30, 2007 5:05 PM by 807592 RSS

generics question

807587 Newbie
Currently Being Moderated
hi,

got this class that extends hashtable:
public class GlobalCache
  extends Hashtable<Integer ,Hashtable<Integer,? extends InfoBean>>
{
  private static GlobalCache _instance = null;  

 static public synchronized GlobalCache instance()
 {
   if (_instance == null)
   {  
     _instance =  new GlobalCache(); 

     _instance.put(123, new Hashtable<Integer,InfoBean>());
     _instance.put(456, new Hashtable<Integer,InfoBean>());
   }
 
    return _instance;
  }
}
when i use the global cache i do the following:
public void someMethod()
{
    GlobalCache global = GlobalCache.instance();
    Hashtable test = global.get(123); // (1)
    test.put(666, new InfoBean());      // (2)
}
the funny thing is that (1) doesn't ask me to parameterize the test Hashtable with
<Integer,InfoBean> but with the normal generic Hashtable parameters <K,V>.
This means i am no restricted to put other objects than <Integer, TrackBean> into test (2).
What's wrong with my generics? Maybe i a m just blind ;)
cheers
/mischka
  • 1. Re: generics question
    807587 Newbie
    Currently Being Moderated
    the funny thing is that (1) doesn't ask me to parameterize the test Hashtable with <Integer,InfoBean> but with the normal generic Hashtable parameters <K,V>.
    Sorry, can you post the precise text of the warning?
  • 2. Re: generics question
    baftos Expert
    Currently Being Moderated
    Do you use the javac -Xlint flag?
  • 3. Re: generics question
    807587 Newbie
    Currently Being Moderated
    This type of code is the fruition of generics - yay - now I can extend various collections and expose my internal data as other various collections (or even collections of collections)
  • 4. Re: generics question
    807587 Newbie
    Currently Being Moderated
    actually eclipse doesn't complain when i put any other objects into the map.
  • 5. Re: generics question
    807587 Newbie
    Currently Being Moderated
    > public class GlobalCache
    
    extends Hashtable<Integer ,Hashtable<Integer,? extends InfoBean>>
    GlobalCache global = GlobalCache.instance();
    Hashtable test = global.get(123); // (1)
    test.put(666, new InfoBean()); // (2)
    would your code work if you initialized test so:
    Hashtable<Integer, InfoBean> test = global.get(123);
    i believe that this way you could still put other classes that have InfoBean as their superclass into that hashtable...
    or evenmore, could you define GlobalChace to extend Hastable <Integer, Hashtable<Integer, InfoBean>>


    PS! i've never used generics, so i may be completely on the wrong tracks.
  • 6. Re: generics question
    807592 Newbie
    Currently Being Moderated
    Hello everybody

    Not sufienciente that declares that your class extends another with the generic should explicitly indicate which types are the ones who accept at the time of instantiating your object cache put you an example of my code

    Hashtable<String,Atributo> salida=new Hashtable<String,Atributo>();
    Atributo elemento = new Atributo();
    salida.put(elemento.getStrNombre(),elemento);

    if u try put another object (try put "3") u have an error message:

    AtributoAD.java:50: cannot find symbol
    symbol : method put(java.lang.String,String)
    location: class java.util.Hashtable<java.lang.String,Atributo>
    salida.put(elemento.getStrNombre(),"3");
    1 error


    I hope it has been for Aid

    kind regards
    Edson

    Edited by: Edson_Chavez on Oct 30, 2007 6:36 PM
  • 7. Re: generics question
    EJP Guru
    Currently Being Moderated
    I hope it has been for Aid
    Well it might have been 2 years and eight months ago when the discussion was happening...
  • 8. Re: generics question
    807592 Newbie
    Currently Being Moderated
    Please tell me how you are helping anyone by trying to answer a 2 and 1/2 year old thread, and when the guy who originally posted the question hasn't been near the forums in over a year? Please stick to recent problems.
  • 9. Re: generics question
    807592 Newbie
    Currently Being Moderated
    ejp wrote:
    I hope it has been for Aid
    Well it might have been 2 years and eight months ago when the discussion was happening...
    well 2 years eight months late, i find this post at google looking for similar problem at generics, maybe someone find too and now this post HAVE a reply to help ;)

    Kind regards
    Edson

    pd:
    i dont know why my screen name has change :( i login to reply and tell me "Please select Screen Name" and when i write "Edson_Chavez" they said "No disponible" :s