Results 1 to 5 of 5

Thread: Hash

  1. #1
    sandy1028 is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default Hash

    Java Code:
     String name=hashm.containsKey("name")?(String)hm.get("name"):"null";

    When the key exists and value doesnot exits it should return null or undefined value and execute with the next line.

    But now It gives run time error and stops execution.

    How to avoid it

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

    Default

    Hashtable will not allow us to store null value.

    If u want to store string a value use "" instead of null

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

    Default code sample

    use HashMap if u want to store null values .for 2 keys I have stored null and try to retreive as what u said.
    import java.util.HashMap;


    class Sample
    {
    public static void main(String[] args) throws Exception
    {
    HashMap ht = new HashMap();
    ht.put("1",null);
    ht.put("2","test1");
    ht.put("3",null);
    String name=ht.containsKey("3")?(String)ht.get("3"):"null ";
    System.out.println(name);


    }//main
    }//Sample

  4. #4
    sandy1028 is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    This code for everything it prints null.
    Java Code:
    import java.util.HashMap;
    class Sample
    {
    public static void main(String[] args) throws Exception
    {
    HashMap ht = new HashMap();
    ht.put("1",null);
    ht.put("2","test1");
    ht.put("3",null);
    String name=ht.containsKey("2")?(String)ht.get("2"):"null";
    System.out.println(name);
    
    }//main
    }//Sample
    I want something like ht.put("3","");
    I mean after the value 3 if there is no character's and if it is a blank space
    This gives run time error.
    Last edited by sandy1028; 04-17-2009 at 08:21 AM.

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

    Default

    Hi ,
    The above program prints the value of key " 2" as "test1" know?
    Could u please tell me what is the input u are giving and what exactly ur output should be so that i can write the code for u.
    -Regards
    Ramya

Similar Threads

  1. Hash Map
    By rekha in forum New To Java
    Replies: 1
    Last Post: 03-21-2009, 01:00 PM
  2. I need a 32 or 64 bit hash function
    By fogus in forum New To Java
    Replies: 12
    Last Post: 03-18-2009, 02:52 AM
  3. Hash Values ???
    By MuslimCoder in forum New To Java
    Replies: 6
    Last Post: 01-16-2009, 04:26 AM
  4. Hash Table Help
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 11-27-2008, 05:12 PM
  5. Hash Table help
    By rhm54 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:25 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
  •