0

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

5
  • 2
    Note that an ArrayList is not the same as an array. Please don't use raw-types, always specify which type your classes should hold, so List<...> and not just List. Same for the second HashMap. You access elements of an ArrayList by using the get method like you did in your demo, where exactly is the problem? Commented Feb 4, 2018 at 15:02
  • 2
    ordersPlaced.get(0) is how you get the first element (which is the map). The string you're seeing is the default implementation of toString defined in the Object class. If you want to print "key, value" pairs, iterate on the result. Commented Feb 4, 2018 at 15:03
  • If you want to interact with the map you may use get(key) like custOrder.get("colour"), it will return "green". Commented Feb 4, 2018 at 15:05
  • Could you have a brief example Zabuza please. That's what I would like to know how to access the colour of the data stored in the array at location 0 for example Maroun. Thanks Commented Feb 4, 2018 at 15:08
  • Zabuza, I understand that, but I then store the map in an array. How to I interact with the maps that are stored in the ordersPlaced array? Thanks Commented Feb 4, 2018 at 16:07

2 Answers 2

4

You're already accessing it.

In order to get the iterate on the map's items, you can:

ordersPlaced.get(0).forEach((key, value) -> {
    System.out.println("Key is: " + key + ", Value is: " + value);
});

Or, earlier to Java 8, something like:

for (Map.Entry<String, String> entry : ordersPlaced.get(0).entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
}

Please note that you should avoid using raw type list. Instead, you should have:

List<Map<String, String>> ordersPlaced = new ArrayList<>(); 

And then:

Map<String, String> m = ordersPlaced.get(0);
Sign up to request clarification or add additional context in comments.

Comments

3

You know it already.

You can get back the stored map by writing

Map<String, String> placedCustOrder = ordersPlaced.get(0);

And avoid using raw types while using List. Declare your list as

List<Map<String, String>> ordersPlaced = new ArrayList<>();

I would like to know how to access the colour of the data stored in the array at location 0

Since you got the map as I said in the line 1

Map<String, String> placedCustOrder = ordersPlaced.get(0);
String colorVal = placedCustOrder.get("colour");

I strongly suggest you to look through Map documentation before proceeding further.

1 Comment

Excellent many thanks. Well go back and look at maps, array etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.