Solved

Hashtable - StringBuffer key

Posted on 2002-06-18
4
279 Views
Last Modified: 2013-12-29
I get get nothing back from the get() calls indicated below.  What gives?


public class HashTableTest {

/**
* Constructor for HashTableTest.
*/
public HashTableTest() {
  super();
}

public static void main(String[] args) {
Hashtable ht = new Hashtable();
 
StringBuffer key1 = new StringBuffer ("key1");
StringBuffer key2 = new StringBuffer ("key2");
   
Thingy thing1 = new Thingy (1, "thing1", 1111);
Thingy thing2 = new Thingy (1, "thing2", 2222);
   
ht.put (key1, thing1);
ht.put (key2, thing2);
   
Thingy thing3 = (Thingy) ht.get(key1); // this works
System.out.println (thing3._i);
System.out.println (thing3._s);
System.out.println (thing3._f);
   
StringBuffer copyKey1 = new StringBuffer("key1");
Thingy thing4 = (Thingy) ht.get(copyKey1);
System.out.println (thing4._i); // java.lang.NullPointerException
System.out.println (thing4._s);
System.out.println (thing4._f);

Thingy thing5 = (Thingy) ht.get("key1");
System.out.println (thing5._i); // java.lang.NullPointerException
System.out.println (thing5._s);
System.out.println (thing5._f);
}
}

public class Thingy {
  int _i;
  String _s;
  final int _f;
   
  public Thingy (int i, String s, int f)
  {
    _i = i;
    _s = s;
    _f = f;
  }
}

0
Comment
Question by:jdaues
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 7090897
StringBuffer.equals() does not behave in the same way as String.equals(). In the javadoc for StringBuffer, you can see that it just inherits the default implementation (Object.equals()), and The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
In other words, two stringBuffer1.equals(stringBuffer2) only if stringBuffer1 == stringBuffer2. If you need to compare, sort, convert or otherwise process a StringBuffer, turn it into a String first.

therefore this would work:
StringBuffer copyKey1 = key1;

copyKey1.equals(key1) == true

but not this:
StringBuffer copyKey1 = new StringBuffer("key1");
copyKey1.equals(new StringBuffer("key1")) == false

convert to string first, then you'll be ok.

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7090907
or just use strings in the first place:

     public static void main(String[] args) {
          Hashtable ht = new Hashtable();
         
          String key1 = "key1";
          String key2 = "key2";
             
          Thingy thing1 = new Thingy (1, "thing1", 1111);
          Thingy thing2 = new Thingy (1, "thing2", 2222);
             
          ht.put (key1, thing1);
          ht.put (key2, thing2);
             
          Thingy thing3 = (Thingy) ht.get(key1); // this works
          System.out.println (thing3._i);
          System.out.println (thing3._s);
          System.out.println (thing3._f);
             
          String copyKey1 = "key1";
         
          Thingy thing4 = (Thingy) ht.get(copyKey1);  // java.lang.NullPointerException
          System.out.println (thing4._i);
          System.out.println (thing4._s);
          System.out.println (thing4._f);
         
          Thingy thing5 = (Thingy) ht.get("key1");
          System.out.println (thing5._i); // java.lang.NullPointerException
          System.out.println (thing5._s);
          System.out.println (thing5._f);
     }
0
 

Author Comment

by:jdaues
ID: 7090955
Thanks, a very thorough answer.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7090960
your welcome :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question