Solved

Hashtable - StringBuffer key

Posted on 2002-06-18
4
276 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

815 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now