Solved

Hashtable - StringBuffer key

Posted on 2002-06-18
4
273 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, a very thorough answer.
0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
your welcome :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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

6 Experts available now in Live!

Get 1:1 Help Now