eHow launches Android app: Get the best of eHow on the go.

How To

How to Work With Java Hash

Member
By techsavy
User-Submitted Article
(0 Ratings)
Java Collection Framework
Java Collection Framework

This article talks about Java's important hashing algorithms like HashSet, HashMap and Hashtable.

Difficulty: Moderately Easy
Instructions
  1. Step 1

    There are 3 important Hash classes available in Java framework. It is very important to understand the when to use what for the better performance of your java application.

  2. Step 2

    HashSet: HashSet is a set of unique elements. Unlike to List classes (ArrayList, Vector etc.), Set classes do not accept duplicate elements. If you try to add the same element again, it will not create any error, but it will overwrite the existing same element.

  3. Step 3

    HashMap: HashMap is a unique class in which you can store key-value pairs of objects. It accepts only one null key. It is not thread-safe, so more than one threads can access and modify the elements of HashMap. As it is not thread-safe, it is faster to access the elements. It is a widely used Hashing algorithm based Java class.

  4. Step 4

    Hashtable: Similar to HashMap, Hashtable can also store key-value pairs of objects. There are two major differences between HashMap and Hashtable.
    1) Hashtable is thread-safe
    2) Hashtable does not accept any null key
    As it is providing extra feature of thread-safe, it should be used very carefully because too much use of Hashtable can lower your application performance because at a time only one thread can access a Hashtable object.

  5. Step 5

    It is very important to decide what you want in your application, thread-safety or better performance. For better performance, use HashMap and for thread-safety, use Hashtable. In List hierarchy, there is one more thread-safe class called Vector. Similar to Hashtable, it provides thread-safety, but it may lower the performance of your application. If you don't need key-value pairs of object, you can use ArrayList (duplicate elements are allowed), Vector (for thread safety, duplicate elements are allowed) or HashSet (duplicate elements are not allowed).

  6. Step 6

    For sample codes of Java packages, visit
    http://java-techsavy.blogspot.com/

    It is an excellent website for Java sample codes. Make sure to visit it.

Resources

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics