?
Solved

Hashtable - StringBuffer key

Posted on 2002-06-18
4
Medium Priority
?
289 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 200 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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