Results 1 to 4 of 4

Thread: Collections

  1. #1
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Post Collections

    Hi,

    I have this code

    <code>
    Map<Long, String> childFileMap = new HashMap<Long, String>();
    childFileMap.put(new Long(1), "aaaaa");
    childFileMap.put(new Long(2), "bbbbbb");
    childFileMap.put(new Long(3), "ccccccccc");

    Iterator childFileMapIterator = (childFileMap.keySet()).iterator();
    while (childFileMapIterator.hasNext()) {

    String jmsChildCorrelationId = childFileMapIterator.next().toString();
    System.out.println("jmsChildCorrelationId========= =="+ jmsChildCorrelationId);

    String childFileName = childFileMap.get(1);
    System.out.println("childFileName===========" + childFileName);
    }
    </code>

    I am not able to understand why this System.out.println("childFileName===========" + childFileName); prints null

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    808
    Rep Power
    6

    Default

    Hi,
    You have stored as Long object as key but you are trying to retreive with primitive long type.

    so retreive like this

    String childFileName = childFileMap.get(new Long(1));
    Ramya:cool:

  3. #3
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks Ramya :)

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

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    You have stored as Long object as key but you are trying to retreive with primitive long type.

    so retreive like this

    String childFileName = childFileMap.get(new Long(1));
    That's not it; the OP supplied a simple '1' which is autoboxed to an Integer, and that's not in the list; change that '1' to '1L' and the key will be correctly autoboxed and found.

    kind regards,

    Jos

Similar Threads

  1. Collections.sort()
    By collin389 in forum Advanced Java
    Replies: 11
    Last Post: 12-31-2009, 11:03 AM
  2. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 09:15 PM
  3. Collections Sort
    By senthil_jr in forum Advanced Java
    Replies: 2
    Last Post: 06-04-2008, 08:11 AM
  4. Collections framework
    By yuvi461 in forum New To Java
    Replies: 2
    Last Post: 01-08-2008, 10:46 AM
  5. Performance Of Collections
    By thomasprabu in forum Advanced Java
    Replies: 0
    Last Post: 01-05-2008, 11: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
  •