Java:使用哈希图,检索所有值并调用方法
我需要存储动态创建的对象的列表,以便可以检索它们并按需调用它们的方法。
据我所见的列表和创建,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然。你可以这样做:
如果你也需要键,你可以只获取键:
或者同时获取键和值:
在所有上述情况下,映射中的每个条目都会被一一遍历。因此,在循环结束时,您将处理完映射中的所有条目。
Sure. You can do this:
If you need the keys too, you can either get just the keys:
or both the keys and values together:
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.
如果 Map 中的所有值都是
Worker
对象,则应将映射声明为Map
类型。这样,当您从地图中提取一个值时,它将被键入为Worker
。这样,您就可以调用在 Worker 上声明的任何方法,而不必在运行时使用instanceof 检查类型。如果映射包含不同的值,并且您需要将值类型保留为
Object
,则使用接口来定义要为每个不同对象类型调用的方法可能会更有利。如果您直到运行时才知道要对这些值运行什么方法,并且映射可以保存不同的值,那么您只需执行当前正在执行的操作,并使用.
Map
Map最后,要获取地图的值,请按照 Chris Jester-Young 在我之前提到的那样进行。正如我之前所说,最大的优点是您的对象将被键入,并且您不需要进行强制转换/实例检查。
If all of the values in the Map are
Worker
objects, you should declare your map to be of typeMap<String, Worker>
. This way, when you pull a value out of the map, it will be typed as aWorker
. This way you can call any method declared onWorker
as opposed to having to check the type at runtime usinginstanceof
.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.
我用它来将 hashMap 中的所有值放入列表中,希望它有所帮助。
I use this to put all values from hashMap on a List, hope it helps.