char question again

char question again

Post by stig » Sun, 15 Feb 2004 06:36:21

hi again.
i have problem using chars, i use java sdk 1.4.2_02 standard edition.

assume i have the two char arrays, that i have created in the following way:

String string1 = new String("abcd");
String string2 = new String("dcba");
char[] word1 = string1.toCharArray();
char[] word2 = string2.toCharArray();

now i want to compare the letters withing each array:
i try this:
if (word1[i] == word2[j]) {
// do something if the two letters are equal.

the problem is that java seems to compare something else then the
letters in the given positions.

i also tried
if ((char)word1[i] == (char)word2[j]) {
// do something if the two letters are equal.

but that did not compare the chars in the given positions.

question 1.
how can i compare the letters in given positions withing the char array?

next problem is that i hoped to use the char arrays as keys in a
HashMap. i begin to realize that if i have a hashmap
HashMap hash = new HasMap(size);
and do:
hash.put(word1, word1);

what i hope for is that the "string" of the characters should form the
key. so that whenever i do a hash.containsKey(word2), i should get true
return if the two char arrays contains the same characters in the same
order. but it seem that what happens is that the address to word1 is
used to for the key, that means that if i change word2 so that it looks
as word1 and then do hash.containsKey(word2), the hash will say that
there is no such key.

question 2.
how can i use the entire length of the char array to form the key in the

i know i could make my life easier by using String objects, however this
in my case is to inefficient, i have a case where i would have to create
and throw half a millions of String objects every second, this is truly
slow compared to doing what i need with chars, (if i get it to work,
that is).

question 3.
where did sun put the documentation about primitives and all operations
that can be made with/on them?
i cannot find it in the API.

again, many thanks

char question again

Post by Ryan Stewa » Sun, 15 Feb 2004 08:35:05

What leads you to believe this?

Just the way you did. Again, what's happening that causes you to think that
it isn't?

Exactly correct. The variables word1 and word2 are pointers to array
objects. If they point at two different objects, how could the two variables
be equal? To compare two arrays, use the static Arrays.equals(oneArray,
otherArray) which is overloaded to take (I believe) any primitive array or
an Object array. To do this along with a HashMap so that you can use
containsKey(), you'd have to extend HashMap and ... possibly overload the
method to take a char[]. Either that or override it and perform a certain
operation based on type of object passed in.


What exactly is this "case" you refer to?

Java Language Specification (1.0 or 2.0) Section 3.12 and Chapters 15 and 16
(specifically from about 15.13 through 16.1).


char question again

Post by Andrew Hob » Sun, 15 Feb 2004 15:03:19


This should work OK.

You do not need to cast to (char). They already are characters.

The problem is the default hashcode of Object typically supplies the address
of the instance in memory. Two different character arrays must therefore
give different hashcodes.

You could convert the character arrays to strings and use the strings as
keys. ie


However you think that this might be too slow. (Have you tried it.
Converting one million char[10] to Strings took about 250 milliSeconds.)

Alternatively you could use a wrapper class and define your own hashcode()
and equals methods(). Not sure that it would be faster though.


MetaSense Pty Ltd -
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


char question again

Post by stig » Sun, 15 Feb 2004 22:02:19


the first problem with the chars is solved, thanks to you people, it was
an error in the code. thanks for asking me looking at it again.

now i will try to explain what i mean with the hash problem i have.
concider the following example:

HashMap hash = new HashMap(25);

String string1 = new String("abc");
String string2 = new String("cba");
char[] word1 = string1.toCharArray();
char[] word2 = string2.toCharArray();

hash.put(word1, word1);
word2[0] = word1[0];
word2[2] = word1[2];

boolean exists = hash.containsKey(word2);
System.out.println(new String(word2) + " exists in list: " + exists );

when doing the .put(word1, word1) i would like the key to be made from
the content of word1 (ie: {'a', 'b', 'c'}) rather then the address to word1.
then when i look up word2 that has been changed to contain the same
letters in the same order (but on a different address) that word1, i
would like the .containsKey() to return true, insetad of false.

so what i am looking for is having the key to be based on the content,
not the address of the word1 and word2.


char question again

Post by stig » Sun, 15 Feb 2004 22:50:05

it did, my fault, there was an error.

this made it. what i did is that i now send the chars to the method that
performs the key lookup in the hashMap. the kay lookup then uses

by sending chars instead of a String to the method that performs the
lookup i cut running time by approximately 30%, now i am happy.


char question again

Post by Ryan Stewa » Sun, 15 Feb 2004 22:59:58


Calling containsKey(x, y) can only return true if x == y or x.equals(y).
Working with arrays, obviously x == y will never be true unless you have two
references to the same array. The same is true of x.equals(y) because arrays
inherit this method from Object, which simply returns x == y. As I mentioned
previously, the proper way to compare two arrays is to use Arrays.equals(x,
y), which takes any primitive array or an Object array. It looks like the
test for equality is actually done in the static eq method of HashMap. If
you were to extend HashMap and override that to check for equal arrays as
well, you could get the behavior you're looking for.

char question again

Post by Daniel Sjl » Mon, 16 Feb 2004 06:24:43

A wrapper class will almost certainly be faster, since
String.valueOf(char[]) will copy the character array. A wrapper does not
require immutability like String.
Daniel Sjlom