Results 1 to 4 of 4
  1. #1
    Joker2012 is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default LinkedList as value in a hashTable Map

    i have to read from a File and store it in a HashTable with the key as String and Value as Linked list.

    my example
    HashTable<String,LinkedList<String>>r = new HashTable<String,LinkedList<String>>();

    when i use the code

    r.put("some String", ??);

    what do i place at "??"

    and what code do i use to display the output

    when i use the code
    LinkedList<String> list = new LinkedList<String>();
    list.addLast("String");
    then
    r.put("some String", list);

    and try to display the entries i get the output as
    some string --> LinkedList@3443

    can you please suggest me any method or code to help me place a linkedlist into a hashtable value....

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: LinkedList as value in a hashTable Map

    I think your code is placing the linkedlist in the hashtable. The following shows that it is working:
    display the entries i get the output as some string --> LinkedList@3443
    The LinkeList class does not have a toString method that creates a display for its contents so it uses the Object class's default toString method which give the classname followed by @ followed by the hashcode.
    To get a prettier display, you will need to write it.

    EDIT:
    The above appears to be wrong!

    I just ran your code and got:
    System.out.println(r); // {some String=[String]}

    How are you displaying the contents of the Hashtable?
    Last edited by Norm; 09-25-2011 at 05:28 PM. Reason: There is toString that formats

  3. #3
    Joker2012 is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: LinkedList as value in a hashTable Map

    im using a for loop

    for (Entry<String, LinkedList<String>> ee: r.entries())
    System.out.println(ee.getKey() + " ==> " + ee.getValue());

    sorry im using m3 as my hashtable
    Last edited by Joker2012; 09-25-2011 at 06:03 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: LinkedList as value in a hashTable Map

    What is m3?
    What package is the class Entry in?
    Last edited by Norm; 09-25-2011 at 06:03 PM.

Similar Threads

  1. hashtable
    By kmm1977 in forum New To Java
    Replies: 4
    Last Post: 01-20-2011, 04:24 PM
  2. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  3. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 08:05 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 04:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Tags for this Thread

Posting Permissions

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