This discussion is archived
1 2 Previous Next 24 Replies Latest reply: Jan 10, 2013 4:28 AM by Kayaman RSS

How to use Generics in a List

689442 Newbie
Currently Being Moderated
I am new to java and have a very basic question about generics.
I have many classes such as Hotel, School, Mall, and many other. They are created by hibernate.
A hql statement coule be :
1, from Hotel t
2, from School
3, from Mall
4, something like that



How to use Generics to simplify code?

Thanks a lot!
  • 1. Re: How to use Generics in a List
    gimbal2 Guru
    Currently Being Moderated
    I don't get it - you're already using generics.

    Hibernate on the other hand does not because it can return just about anything - it all depends on what you ask of it in the HQL query which might just end up returning a List of Object arrays where each index holds an object of a different class. That's why you need the manual typecasts, only you know what you are asking Hibernate to return.

    And please: use \
     tags when posting code.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 2. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    If I have many Classes, I must write more code such as else if.
    For example, how to simply the following method.
    Can I use List<T> and T only equals to Hotel, School, Mall, and some other classes
    Thanks again.
  • 3. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    For example, how to simply the following method.
    I would get rid of the whole method. It's horribly designed. For one, you're passing it a String argument, to determine which type it will work on.
    Secondly, you're using all that code just to check if an entity exists for that id.
    Thirdly, why do you need a separate method to see whether an entity exists for a particular id? What's the use case?

    You have a design problem, one that can't be solved with generics.
  • 4. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    1, It is a method of web services. Mobile applications only send String, int, long values to web services. Type means which selection is chosed from mobile users and I will use this type to find a table entity to save, delete, or update.

    2+3, I must check the type because mobile users may send wrong values. Mobile applications are not finished yet.

    Yes, it's horribly designed. Please give me some recommendations and expamples. Please do not use examples such as animal class, cat class, shape class.

    Thanks again.
  • 5. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    1, It is a method of web services. Mobile applications only send String, int, long values to web services. Type means which selection is chosed from mobile users and I will use this type to find a table entity to save, delete, or update.
    Fair enough, in many (if not most) use cases you wouldn't need to just check for a value in a database, there would be other work involved in the same transaction as well, that's why I asked.
    Yes, it's horribly designed. Please give me some recommendations and expamples.
    Well, for the simplest option, I would create 3 separate methods and decide which one to call on a higher level.
    Sure you'd have 3 almost identical methods, but it's still better than one method that has 3 almost identical parts and a String parameter to decide between them.
    Trying to cram in some "smart" solution isn't always the best choice, especially considering that you only have 3 different entities to check for (or will you have more?).
    Please do not use examples such as animal class, cat class, shape class.
    Uh...okay.
  • 6. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    I have more than 30 tables at this stage.
  • 7. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    I have more than 30 tables at this stage.
    Then you could create a method that maps incoming data to the table name. It can be as easy as converting "HOTEL" to "Hotel" (I hope you'll be able to do that by yourself), but the method could also allow other names.

    Hmm, now I notice that your tables don't have id fields (or at least they're not used), instead they're named "hotelid", "spotid" etc. That'll require you to map the name of the primary key as well.

    Anyhow, then you can just get the mapped entity name and primary key name from the method, and insert them to your HQL query. That'll cut down on the code a bit.
    Of course in this case, you'll actually get rid of the generics, since you'll be using the same code for all entity types. Not that you needed the generics in the first place, since you're just checking to see if a list is empty, not what's in it.
  • 8. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    These table entitys are not created by me. They are create by another person for a running application. Mobile functions are just start from this month. I cannot change these table entity (eo).
    Could you recommend some better way to reduse if-else clause?

    Thanks again!

    Edited by: YE on Jan 10, 2013 7:02 PM
  • 9. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    1, How to defing List<?> for this method based on incomming data?
    What do you mean? That's exactly the syntax.
    List<?> results = session.createQuery(hql).list();
    2, If else clause is to much. Could you recommend some better way to reduse if-else clause?
    You could initialize a Map with the type as the key and the table name and id name as the value.
  • 10. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    Except IdentityHashMap, which map can use one key to save more than one values?
  • 11. Re: How to use Generics in a List
    EJP Guru
    Currently Being Moderated
              IdentityHashMap<String,Object> mapIni = new IdentityHashMap<String,Object>();
    I don't know why you think you need an IdentityHashMap, but you don't. A normal HashMap will do.
              mapIni.put(new String("Hotel"), "Hotel");
              mapIni.put(new String("Hotel"), "hotelid");
    You don't need the first of those at all, and the second one should be:
    mapIni.put("Hotel", "hotelid");
    Don't waste space creating new Strings around string literals.
              for (Entry<String, Object> entry : mapIni.entrySet()) {
                    if(entry.getKey().equalsIgnoreCase(type)){
                           String val = entry.getValue().toString();
    It's a complete and utter waste of time iterating the Map when it already has a get() method. All you need is
    String val = mapIni.get(type);
    and make sure you don't have wrong-cased versions of the types hanging around.
  • 12. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    I use IdentityHashMap because every key has 2 values.

    If I use HashMap, can I extends it as follows?

    class MyHashMap extends HashMap  
    {  
        @Override  
        public Object put(Object key, Object value)  
        {  
    
            if(!this.containsKey(key))  
                return super.put(key, value);  
              
            return null;  
        }  
    } 
  • 13. Re: How to use Generics in a List
    980759 Newbie
    Currently Being Moderated
    than use ArrayList insteadof Object,
    HashMap<String,ArrayList>
  • 14. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    When should I use IdentityHashMap?
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points