There are a few different types of maps. I’ve used HashMap and TreeMap, but I have yet had the need to use a LinkedHashMap. The ordering of the elements in each differ a bit depending on the type of map.
Type Order
---------- ---------------------------------------------------------------
HashMap The location of the entries added to a HashMap is unpredictable
in that it is governed by a hashing algorithm.
TreeMap Order is by key
LinkedHashMap The order of the entries in a LinkedHashMap is governed by the
previously entered element to which it is linked.
The method example below will print to the console the key/value pairs contained within an arbitrary map. The method can work with all Maps.
Object Description
-------------- ---------------------------------------------------
aHashMapOfObject This object is your HashMap containing objects
placed within it by using the HashMap’s put()
method.
pairs Contains the key/value pairs. These values will
mirror the values used on the HashMap’s put()
method.
key The value of the key used for the object placed
into the HashMap.
yerob This is the object initially placed into the
HashMap.
Creating the HashMap and then adding a key/value pair would then look like this:
NameAndAddr nameAddr = new NameAndAddr("Imgona Choitoya");
HashMap aHashMapOfObjects = new HashMap();
aHashMapOfObjects.put("NameAddr01", nameAddr);
Iterator iter = aHashMapOfObjects.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry pairs = (Map.Entry)iter.next();
String key = (String)pairs.getKey();
NameAndAddr yerob = (NameAndAddr)pairs.getValue();
String name = yerob.getName();
System.out.println("The key value within yerob is " + key);
System.out.println("The value of the property within the object is " + name );
}
This would print:
The key value within yerob is NameAddr01
The value of the property within the object is Imgona Choitoya
For completeness, the NameAndAddr class and the TestClass class is shown.
NameAndAddr.java
package testpackage;
public class NameAndAddr {
private String name;
/**
* Constructor
* @param value
*/
public NameAndAddr(String value) {
this.name = value;
}
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
}
TestClass.java
package testpackage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestClass {
/**
* @param args
*/
public static void main(String[] args) {
TestClass test = new TestClass();
test.runIt();
}
/**
*
*
*/
public void runIt() {
NameAndAddr nameAddr = new NameAndAddr("Imgona Choitoya");
HashMap aHashMapOfObjects = new HashMap();
aHashMapOfObjects.put("NameAddr01", nameAddr);
Iterator iter = aHashMapOfObjects.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry pairs = (Map.Entry)iter.next();
String key = (String)pairs.getKey();
NameAndAddr yerob = (NameAndAddr)pairs.getValue();
String name = yerob.getName();
System.out.println("The key value within yerob is " + key);
System.out.println("The value of the property within the object is " + name );
}
}
}
No comments:
Post a Comment