This discussion is archived
1 2 Previous Next 24 Replies Latest reply: Jan 10, 2013 4:28 AM by Kayaman Go to original post RSS
  • 15. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    When should I use IdentityHashMap?
    You shouldn't. Don't worry about that class, if you need it, you'll be experienced enough to know.

    As for having several values per key, you need to put the values in another object, like a list or so.
    ArrayList<String> foo = new ArrayList<>();
    foo.add("Hotel");
    foo.add("hotelid");
    map.put("Hotel", foo);
    Also I wouldn't recreate the map every time in the method, you only need to put the values in there once, so how about initializing it somewhere else.
  • 16. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    I use IdentityHashMap because every key has 2 values.
    That doesn't make any sense. IdentityHashMap only affects how the keys are handled, not the 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;  
    }  
    } 
    You can, except it wouldn't do anything useful. Quite the opposite actually, you wouldn't be able to put anything in the map.

    May I ask how you came to be in charge of this programming part? The code you're working on is beyond beginner level, but you seem to be a beginner in Java?
  • 17. Re: How to use Generics in a List
    980759 Newbie
    Currently Being Moderated
    this is how the below map works,
    IdentityHashMap uses equality operator(==) for comparing the keys (if(key1==key2))
    and HashMap uses equals() method if(key1.equals(key2))

    http://docs.oracle.com/javase/7/docs/api/java/util/IdentityHashMap.html
    Map identityMap = new IdentityHashMap();
    Map hashMap = new HashMap();
    identityMap.put("Daya", 1);
    identityMap.put(new String("Daya"), 2);
    identityMap.put("Daya", 3);
    
    hashMap.put("Daya", 1);
    hashMap.put(new String("Daya"), 2);
    hashMap.put("Daya", 3);
    System.out.println("IdentityMap KeySet >> " +  identityMap.keySet().size());
    System.out.println("HashMap KeySet  >> " + hashMap.keySet().size());
  • 18. Re: How to use Generics in a List
    gimbal2 Guru
    Currently Being Moderated
    Kayaman wrote:
    May I ask how you came to be in charge of this programming part? The code you're working on is beyond beginner level, but you seem to be a beginner in Java?
    Give this a read for a laugh:

    http://thedailywtf.com/Articles/The_Brillant_Paula_Bean.aspx

    I've had some discussion about if this is actually a true story or not - I for one believe it.
  • 19. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    Can you give me some code examples of using IdentityHashMap?

    Edited by: YE on Jan 10, 2013 7:01 PM
  • 20. Re: How to use Generics in a List
    EJP Guru
    Currently Being Moderated
    I use IdentityHashMap because every key has 2 values.
    But you don't need two values for every key, when one of them is the same as the key. I've already told you that.
    When should I use IdentityHashMap?
    You're getting off the track. Your question is about 'How to use Generics in a List'.
    Can you give me some code examples of using IdentityHashMap?
    Why? You don't need it for the purposes of this question. I've been using Java for over 15 years and I've never used it at all.
    I am a beginner and I find this forum for java beginner.
    And you aren't taking the slightest notice of anything you're being told in it. What's the purpose of posting exactly?
    His code has about 50 if-else clause to decide where should the incomming data send to.
    Crummy code can come from anywhere, even yourself, and I speak as an authority ;-)
    For example, the incomming data like: <root><method>SubmitOrder</method><params><param>userid</param><param>1</param><param>Hotel</param></params></root>.
    If-else clauses as follow :
    if.............
    else if("SubmitOrder".equals(methodName)){
    SubmitOrder(Long.parseLong(s[1]), Long.parseLong(s[2]), s[3], Long.parseLong(s[4]),...............
    ..........................
    So it seems you are expected to use Reflection to solve this problem. Not an IdentityHashMap. Why don't you talk to your colleague to sort this out?
    At this stage, this *.java file has more than 5000 lines.
    At this stage the file is already close to its limit. Somebody somewhere is going to have to redesign and reimplement this mess. I suggest you hand the assignment back where it came from and ask for something that is actually feasible.
  • 21. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    I cannot get help in my team. That's why I post thread here.
  • 22. Re: How to use Generics in a List
    689442 Newbie
    Currently Being Moderated
    The code you're working on is beyond beginner level, but you seem to be a beginner in Java:
    Do you mean who web service code or map is beyong beginner level?
  • 23. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    I cannot get help in my team. That's why I post thread here.
    Well if you're asking questions like these, I'm not wondering why.
    Forget IdentityHashMap. Forget it! YOU DON'T NEED IT. If you want help, don't make it hard for us to help you. Also, I'd suggest ignoring Dayananda B V, he is only muddying the waters, talking about things you don't need.


    As I said, since a Map can have only one value per key, you need to use a class that can contain multiple values as the map's value. I even gave you a code example, with an ArrayList as the value.

    As for EJP's comment about not needing 2 values, this would be in case the message from the wire doesn't match the name of the entity.
  • 24. Re: How to use Generics in a List
    Kayaman Guru
    Currently Being Moderated
    YE wrote:
    The code you're working on is beyond beginner level, but you seem to be a beginner in Java:
    Do you mean who web service code or map is beyong beginner level?
    Well, let's just say that clearly this is out of your league. The code itself is nothing special, but you seem to be lacking in the basics.
1 2 Previous Next

Legend

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