Fairly New to java(need help) on concepts:Hashmaps

It helps to compare the function of a HashMap with an ArrayList. Suppose you want to find a specific element in an ArrayList. Your only option is the scan the entire list in order until you find the one that you want:

ArrayList<String> arString = new ArrayList<String>();
// populate the list with a series of String objects
public boolean isElementInList(String elementToFind) {
    for(String s: arString) {
        if(s.equals(elementToFind)) {
            return(true);
        }
    return(false);
}

This will work, but what happens if the list is very large? It will take a long time to find the element that you need.

The Hash solves this problem. Essentially, a Hash will generate a number that it can use to figure out more-or-less where the element is that it is looking for, instead of having to scan every element. A Map, on the other hand, is a structure that associates keys with values. You can think of an ArrayList as a kind of a map that associates Objects (the elements in the array) with an integer (the array index). In Map terminology, the array element is a 'value' and the array index is the 'key'. A Map builds on this concept, but will let you associate a value with a key of any type. So, instead of using an integer as a key, a Map will allow you to use a String, or any object, in fact.

A HashMap, then, combines these two concepts. It defines a key and value relationship, and uses a hash to quickly locate the values by performing a function on the key known as 'hashing'. In crude terms, hashing will turn the key object into an integer (known as the hash value). Two identical key objects will produce the same hash.

When a HashMap is instantiated, you will declare the key and value types that you want. Suppose we have an Account object that we need to associate with an Account Number:

public class Account {
    String name;
    String address;
}

public class AccountNumber {
    String AccountNumber;
}

You could now define a HashMap as follows:

HashMap<AccountNumber, Account> myHashMap = new HashMap(<AccountNumber, Account>);

Values are placed into the HashMap using the put method:

AccountNumber accountNumber = new AccountNumber(...);
Account account = new Account(...);

myHashMap.put(accountNumber, account);

Retrieving a value is done with the get method:

Account account = myHashMap.get(accountNumber);

One very important point - if you define your own class for the key, you must override the hashCode() method. This method will be called by the HashMap in order to hash the key value. Java provides default hash codes for some objects (String, Integer etc.) but if you create your own object, you must provide a hashCode() method. Also, you will have to override the equals() method. The HashMap will use this method to determine if two key objects truly are the same. The reason for this is different key objects can result in the same hash code. The HashMap will first hash the key using the hashCode() method, and then call the equals() method on all objects that match that hash code to find the one that you are really looking for.

/r/javahelp Thread