learning Java and have figured out how to store a hashmap in an array. But I can't figure out how to get to the stored data. Here is a simplified version of what I'm doing. I've got as far as displaying the specific array items, but how do I access the hash map stored in the array?
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Map<String, String> custOrder = new HashMap<String, String>();
List ordersPlaced = new ArrayList();
custOrder.put("colour", "blue");
custOrder.put("manu", "bmw");
custOrder.put("body", "4x4");
ordersPlaced.add(custOrder);
custOrder = new HashMap();
custOrder.put("colour", "green");
custOrder.put("manu", "merc");
custOrder.put("body", "saloon");
ordersPlaced.add(custOrder);
System.out.println(ordersPlaced.get(0).toString());
}
}
Hope that makes sense. Thanks in advance
Neil
ArrayListis not the same as an array. Please don't use raw-types, always specify which type your classes should hold, soList<...>and not justList. Same for the secondHashMap. You access elements of anArrayListby using thegetmethod like you did in your demo, where exactly is the problem?ordersPlaced.get(0)is how you get the first element (which is the map). The string you're seeing is the default implementation oftoStringdefined in theObjectclass. If you want to print "key, value" pairs, iterate on the result.get(key)likecustOrder.get("colour"), it will return"green".