Java:使用哈希图,检索所有值并调用方法

发布于 2024-11-15 16:05:46 字数 539 浏览 7 评论 0原文

我需要存储动态创建的对象的列表,以便可以检索它们并按需调用它们的方法。

据我所见的列表和创建,HashMap 满足我的需求,但我对使用 HashMap 调用对象和调用它们的方法有点困惑。

作为参考,让我给您一些代码:

这是 HashMap:

Map<String, Object> unitMap = new HashMap<String, Object>();

// here is how I put an object in the Map notice i'm passing coordinates to the constructor:
unitMap.put("1", new Worker(240, 240));
unitMap.put("2", new Worker(240, 240));

现在我需要创建一个方法来检索 hashmap 中的每个对象并从每个对象调用一个方法。这是可能的还是只能直接引用创建的对象。如果是这样,是否有另一种方法可以动态调用类的所有现有实例的方法(换句话说,根据用户输入)?

I have a need to store a list of dynamically created objects in a way where they can all be retrieved and their methods called on demand.

As far as I can see for the list and creation, a HashMap fits my needs but i'm a bit puzzled on recalling the objects and calling their methods using the HashMap.

Just as a reference, let me give you a little code:

Here is the HashMap:

Map<String, Object> unitMap = new HashMap<String, Object>();

// here is how I put an object in the Map notice i'm passing coordinates to the constructor:
unitMap.put("1", new Worker(240, 240));
unitMap.put("2", new Worker(240, 240));

Now I need to create a method that retrieves every object in the hashmap and call a method from each object. is this possible or can the created objects only be referenced directly. If so, is there another way to call a method of all existing instances of a class dynamically (in other words, on user input)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再可℃爱ぅ一点好了 2024-11-22 16:05:46

当然。你可以这样做:

for (Object thing : unitMap.values()) {
    // use "thing" here
}

如果你也需要键,你可以只获取键:

for (String key : unitMap.keySet()) {
    // use "key" here
}

或者同时获取键和值:

for (Map.Entry<String, Object> entry : unitMap.entrySet()) {
    // use "entry.getKey()" and "entry.getValue()"
}

在所有上述情况下,映射中的每个条目都会被一一遍历。因此,在循环结束时,您将处理完映射中的所有条目。

Sure. You can do this:

for (Object thing : unitMap.values()) {
    // use "thing" here
}

If you need the keys too, you can either get just the keys:

for (String key : unitMap.keySet()) {
    // use "key" here
}

or both the keys and values together:

for (Map.Entry<String, Object> entry : unitMap.entrySet()) {
    // use "entry.getKey()" and "entry.getValue()"
}

In all the above cases, each entry in the map is traversed one by one. So at the end of the loop, you'll have processed all the entries in the map.

小忆控 2024-11-22 16:05:46

如果 Map 中的所有值都是 Worker 对象,则应将映射声明为 Map 类型。这样,当您从地图中提取一个值时,它将被键入为 Worker。这样,您就可以调用在 Worker 上声明的任何方法,而不必在运行时使用instanceof 检查类型。

如果映射包含不同的值,并且您需要将值类型保留为Object,则使用接口来定义要为每个不同对象类型调用的方法可能会更有利。

如果您直到运行时才知道要对这些值运行什么方法,并且映射可以保存不同的值,那么您只需执行当前正在执行的操作,并使用 MapMap.

最后,要获取地图的值,请按照 Chris Jester-Young 在我之前提到的那样进行。正如我之前所说,最大的优点是您的对象将被键入,并且您不需要进行强制转换/实例检查。

If all of the values in the Map are Worker objects, you should declare your map to be of type Map<String, Worker>. This way, when you pull a value out of the map, it will be typed as a Worker. This way you can call any method declared on Worker as opposed to having to check the type at runtime using instanceof.

If the map holds different values, and you need to keep the value type as Object, it may be advantageous to use an interface to define the method that you want to call for each different object type.

If you do not know what method you want to run on the values until runtime, and the map can hold different values, you will just have to do what you are currently doing, and use Map<String, Object>.

Finally, to get the values of the map, you do just as Chris Jester-Young mentioned before me. The biggest advantage, as I said previously, is that your objects will be typed, and you will have no need for casting/instanceof checking.

时光倒影 2024-11-22 16:05:46

我用它来将 hashMap 中的所有值放入列表中,希望它有所帮助。

private List<String> getValuesFromHashMap(HashMap<String, String> hashMap) {

    List<String> values = new ArrayList<String>();

    for (String item : hashMap.values()) {
        values.add(item);
    }

    return values;
}

I use this to put all values from hashMap on a List, hope it helps.

private List<String> getValuesFromHashMap(HashMap<String, String> hashMap) {

    List<String> values = new ArrayList<String>();

    for (String item : hashMap.values()) {
        values.add(item);
    }

    return values;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文