Solved

Hashtable - StringBuffer key

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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…
This video teaches viewers about errors in exception handling.

929 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

12 Experts available now in Live!

Get 1:1 Help Now