The Soul
08-27-2009, 06:05 AM
Hello there! This thread will be all about teaching HashMaps to the users, meaning you. First off, I'd like to state that HashMaps are no replacement for ArrayLists as they don't do the same thing, yet they're very similar at the same time. HashMaps have to include of both a key and value because of the Map object. In order to use this HashMap, you're going to have to import this package:
import java.util.*;
This loads all of the classes within the util package, but if you want to be specific, you'll just load these objects of the util package:
import java.util.HashMap;
import java.util.Map;
HashMaps use generics as ArrayLists do (I'm not absolutely positive if you need generics, but I've been told after the JDK 5.0 update you need to use generics) - another similarity right there. The declaration of the object ' that loads the HashMap when called is shown below:
public HashMap<String, Integer> newObject = new HashMap<String, Integer>();
Using the 'new' keyword, we've created a new object called 'newObject' that loads this HashMap.
In order to store anything within this, you need to use the put() method like so:
public HashMap<String, Integer> newObject = new HashMap<String, Integer>();
newObject.put("str", 0);
The newObject HashMap now consists of the String 'str' at the value of 0.
Say you wanted to recieve the String 'str' in the newObject HashMap; you can use the get() method to do so. For example, if I wanted to print 'str' into the command prompt, I would do this:
String printstr;
printstr = newObject.get(0));
System.out.println(printstr);
The remove() method is useful for removing things in the HashMap when needed.
newObject.remove(0);
This would remove the String 'str' from the HashMap.
In conclusion, I hope you've at least learned the basics of HashMaps whilst reading this tutorial.
import java.util.*;
This loads all of the classes within the util package, but if you want to be specific, you'll just load these objects of the util package:
import java.util.HashMap;
import java.util.Map;
HashMaps use generics as ArrayLists do (I'm not absolutely positive if you need generics, but I've been told after the JDK 5.0 update you need to use generics) - another similarity right there. The declaration of the object ' that loads the HashMap when called is shown below:
public HashMap<String, Integer> newObject = new HashMap<String, Integer>();
Using the 'new' keyword, we've created a new object called 'newObject' that loads this HashMap.
In order to store anything within this, you need to use the put() method like so:
public HashMap<String, Integer> newObject = new HashMap<String, Integer>();
newObject.put("str", 0);
The newObject HashMap now consists of the String 'str' at the value of 0.
Say you wanted to recieve the String 'str' in the newObject HashMap; you can use the get() method to do so. For example, if I wanted to print 'str' into the command prompt, I would do this:
String printstr;
printstr = newObject.get(0));
System.out.println(printstr);
The remove() method is useful for removing things in the HashMap when needed.
newObject.remove(0);
This would remove the String 'str' from the HashMap.
In conclusion, I hope you've at least learned the basics of HashMaps whilst reading this tutorial.