0

I have a problem in JAVA when i'm trying to return a HashMap that I have added to a list of type: List<Object>. I know I can use other type of lists, but I need to use List<Object>

List<Object> listOfObjects = new ArrayList<Object>();

    HashMap<String, String> hashmap = new HashMap<String,String>();
    hashmap.put("x", "foo");
    hashmap.put("y", "bar");

    listOfObjects.add(hashmap);

    for (int i = 0; i < listOfObjects.size(); i++) {
        System.out.println(listOfObjects.get(i));
    }

I have added my hashmap to my listOfObject, but how do I get the HashMap from the listOfObject such that I can use the HashMap-commands. fx: hashmap.get("x) and it will return "foo".

Normally i thought i could just write: listOfObjects.get(0).get("x") and it would return "foo" but that does not work.

If anyone know another work around that's find but I just need to use a List.


Normally i thought i could just write: listOfObjects.get(0).get("x") and it would return "foo" but that does not work.

No, it wouldn't - because the type of listOfObjects.get(0) is just Object. How do you expect the compiler to know that it's meant to be a map?

You can use:

HashMap<String, String> map = (HashMap<String, String>) listOfObjects.get(0);
// Use map...

... but be aware that due to the nature of generics in Java, that cast isn't really ensuring that all the key/value pairs in the map are "string to string". The cast would work even if you'd originally used:

Map<Integer, Integer> badMap = new HashMap<Integer, Integer>();
badMap.put(0, 10);
listOfObjects.add(badMap);

You'll get a warning for this, but it's important to understand what it means. It's not clear what your use case is, but if you can make it more strongly typed (perhaps create a new class which contains a Map<String, String>?) that would be good. Is every element of your list going to be a map? If so, why are you using List<Object> rather than a more strongly-typed list? If some elements aren't going to be maps, how can you tell which ones will be? (These are the sort of things you should be thinking about carefully.)

4

2 回答 2

3

通常我认为我可以写:listOfObjects.get(0).get("x") 它会返回“foo”,但这不起作用。

不,它不会 - 因为类型listOfObjects.get(0)只是Object. 您如何期望编译器知道它是一个地图?

您可以使用:

HashMap<String, String> map = (HashMap<String, String>) listOfObjects.get(0);
// Use map...

...但请注意,由于 Java 中泛型的性质,这种转换并不能真正确保映射中的所有键/值对都是“字符串到字符串”。即使您最初使用过,演员表也会起作用:

Map<Integer, Integer> badMap = new HashMap<Integer, Integer>();
badMap.put(0, 10);
listOfObjects.add(badMap);

你会收到一个警告,但理解它的含义很重要。目前尚不清楚您的用例是什么,但是如果您可以使其类型更强大(也许创建一个包含 ? 的新类)Map<String, String>那就太好了。您列表中的每个元素都会成为地图吗?如果是这样,你为什么使用List<Object>而不是更强类型的列表?如果某些元素不会成为地图,您如何判断哪些元素会成为地图?(这些是你应该仔细考虑的事情。)

于 2013-02-11T13:12:14.970 回答
1

I hope this will help u..

List<Object> listOfObjects = new ArrayList<Object>();

        HashMap<String, String> hashmap = new HashMap<String,String>();
        hashmap.put("x", "foo");
        hashmap.put("y", "bar");

        listOfObjects.add(hashmap);

        for (int i = 0; i < listOfObjects.size(); i++) {
            System.out.println(((HashMap<String, String>)listOfObjects.get(i)).get("x"));
        }

Normally as your list is of type of object . so first cast it to HashMap type and then get the value from map

please notice the following code

 System.out.println(((HashMap<String, String>)listOfObjects.get(i)).get("x"));
于 2013-02-11T13:17:59.327 回答