IT Blog NOW

An Information Technology View by Sam Moreira

Google Accounts on Twitter

Over the weekend, the Google Blog team posted a list of Google accounts on Twitter. I had no idea Google had so many twitter accounts. If all other companies also posted some sort of directory of where we could find what is interesting to us about them, it would make it easier for us to be updated.

Some of the Google accounts listed are:

 For the complete list, check their blog post at Google Blog.

My Lucky Day?

Something happened to me Yesterday that I would think it would only happen in movies. Yesterday, I parked my car to get my mail, and when I came back to drive off I’ve noticed that a fire truck and an ambulance had blocked my way. Soon I realized I had parked between two cars that were involved in an accident before I got there.

If that wasn’t enough, I have worked before with one of the people involved in the accident. So, I walked to his car to talk to him and his wife, and he told me what had happened. He seemed ok, but, as he had a heart attack a few months ago, they had to take him to the hospital just to make sure he was ok.

I was probably there waiting for them to unblock my car for at least 40 minutes and during all this time, the people on the other car were looking at me as if I was their worst enemy; and I don’t even know them. I guess it was because I was talking to my friend.

Finally, 40 minutes later, the fire truck was moved and I was able to drive out of there. Do you think that is all? Wait there’s more… Considering all my luck and the time at that point, there was no place to park in front of my building so I tried to find a spot behind it. When I finally drove around the building and parked on an empty spot, who do you think I see? I see the people involved in the accident that were killing me with their eyes. When they looked through my windshield and recognized me, they gave a very angry look. At this time, I heard one of them saying “look, he’s following us”.

As I knew I would not be able to convince them it was all a coincidence, I just got out the car and walked home. But when I got home, I was afraid that something could happen to my car that was parked next to their place. You know, anger plus easy target is not good combination. So, I went back and moved the car to a safer place.

After a tough day at work, I go home and have to deal with all this madness. Things like these only happen to me.

How To Use Custom Objects as Hashtable Keys Without Getting Duplicated Keys in Your Collection

Have you ever faced an issue of having duplicated key objects in your Hashtable even though the objects represent the same entity? In this post I’ll show a way to circumvent the Hashtable behavior.

Sometimes we have a need to use objects as Hashtable keys instead of strings. At least for me, most of the time, Hashtables are used as standard dictionaries such as in the sample below:

Hashtable Sample with String Keys

The code above produces the following outcome:

Hashtable with String Keys results

In the sample above, the value of “key2” key has been updated as expected. Also notice that the Hash value of key2 is the same on both DisplayResults() executions.

Now, what happens if instead of using a string as the key, we use an object? Here’s the updated code after we change the key from string to a custom object:

Hashtable class with Objects as Keys

And here’s the outcome:

Hashtable with objects as keys results

As you can see by the resulting outcome above, when I tried to update the instance of User 123, the Hashtable actually added the object as a new item to the collection and updated its value instead.

Why does this happen? Actually, the results of the second test are the ones we should expect when working with Hashtables as each newly created object receives a new hashcode value, and the Hashtable uses the hashcode value to search for keys in the collection. So, when the new user was created to update the value of the existing one, the new hashcode value given to the new object was used to search for an existing item in the collection; and as it could not be found, the object was added as a new item in the collection.

But, why doesn’t this happen with strings? They’re also objects, correct? String objects have a different behavior as they override object class methods to always consider equal two strings holding the same values even if they are actually different instances. On the first code sample, Let’s change the statement that updates the value of key2 to the following:

Hashtable Statement with string case changed

After running the program, we’ll get the following results:

Hashtable with different case strings used as keys

This result looks like the second test results when a custom object was used as the key; only at this time, we used a string. Why didn’t we get the same results as our first test? Notice that I have updated the “k” (lowercase) to “K” (uppercase) to make the values of each string object different. So, if you look at the resulting outcome, the “Key2” string has a different hashcode value than the “key2” one, what means that the overridden GetHashCode() string method didn’t come into play and the Hashtable behaved as it should.

Now that we have covered the basics, how do I make my custom object behave like the string object to prevent duplication of objects representing the same entity in my collection? Simple, we’ll have to update our User class to override two methods of the object class: GetHashCode() and Equals().

Here’s the new User class with the overridden methods:

Custom User class with overridden object methods

In case you’re lost, we’re doing two things here: first, we’re overriding the GetHashCode() method to return the hashcode of the UserCode property value (a string) instead of the hascode of the User instance itself. This way, two User instances with the same UserCode value will have the same hashcode value. And second, we’re overriding the Equals() method to use the value of the UserCode property for comparison; this way the class can identify whether or not two instances of the User class represent the same entity.

Here is the outcome of our program after the two object methods have been overridden:

User class with overridden methods results

It’s nice isn’t it? Now we can easily update the value part of User keys in the collection without worrying that duplicated User keys will be added to the collection.

Enjoy!