Java地图带有多个值:容器类还是多个地图?

huangapple go评论82阅读模式
英文:

Java map with multiple values: container class or multiple maps?

问题

我正在尝试创建一个映射,其中UUID键指向一个对象(例如某个User类实例)和有关该对象的辅助信息(例如一个int和一个String)。有两种明显的实现方式:

第一种方式:

import java.util.Map;
import java.util.HashMap;
import java.util.UUID;
...
    Map<UUID, User> main = new HashMap<UUID, User>();
    Map<UUID, Integer> aux1 = new HashMap<UUID, Integer>();
    Map<UUID, String> aux2 = new HashMap<UUID, String>();

第二种方式:

import java.util.Map;
import java.util.HashMap;
import java.util.UUID;
...
    private static class UserInfo { // 嵌套类
        public final User user;
        public final int aux1;
        public final String aux2;

        public UserInfo(User user, int aux1, String aux2) {
            this.user = user;
            this.aux1 = aux1;
            this.aux2 = aux2;
        }
    }
...
    Map<UUID, UserInfo> main = new HashMap<UUID, UserInfo>();

就效率和内存使用而言,哪种方式通常被认为更具性能,或者是否有更值得的第三种选择?

英文:

I'm trying to create a mapping in which a UUID key points to both an object (e.g. some User class instance) and auxiliary information about that object (e.g. an int and a String). There are two obvious implementations of this:

import java.util.Map;
import java.util.HashMap;
import java.util.UUID;
...
    Map&lt;UUID, User&gt; main = new HashMap&lt;UUID, User&gt;();
    Map&lt;UUID, Integer&gt; aux1 = new HashMap&lt;UUID, Integer&gt;();
    Map&lt;UUID, String&gt; aux2 = new HashMap&lt;UUID, String&gt;();

or

import java.util.Map;
import java.util.HashMap;
import java.util.UUID;
...
    private static class UserInfo { // Nested class
        public final User user;
        public final int aux1;
        public final String aux2;

        public UserInfo(User user, int aux1, String aux2) {
            this.user = user;
            this.aux1 = aux1;
            this.aux2 = aux2;
        }
    }
...
    Map&lt;UUID, UserInfo&gt; main = new HashMap&lt;UUID, UserInfo&gt;();

Which is generally considered more performant, in terms of efficiency and memory usage? Or is there a more worthwhile third option?

答案1

得分: 2

出于内存和效率考虑,我认为第二个选项是更好的解决方案。第一个解决方案会创建更多使用内存的“Maps”,并存储使用内存的相同“UUID”。

这也更有利于可维护性和可读性,除非毫秒级性能至关重要,否则这些方面更为重要。如果将相关信息捆绑在一起,更容易确定信息来源以及其描述内容。

还要考虑项目的未来发展,例如为用户添加另一个像aux3的描述符。对于第一个示例,您需要添加另一个哈希映射,记得向新哈希映射中添加数据,并记得从中提取数据,就像您从其他地方提取数据一样。这将在初始设置和访问时创建大量样板代码。

uuid = //一些uuid
Map<UUID, Double> aux3 = new HashMap<UUID, Double>();

// 一些代码
aux3.put(uuid, 1.0);

// 一些代码 ...
aux3.get();

但是使用组合方法,您只需要记得添加类属性(带有后继和变异器),然后可以在任何地方自由使用这些属性。每次从用户映射中访问时,您只需要调用get(uuid),就可以访问所有关联数据。

英文:

For both memory and efficiency, I believe the second option is the better solution. The first solution is creating more Maps which use memory, and storing the same UUID which use memory.

It is also better for maintainability and readability, which unless performance to the millisecond is critical, is the more important of these. If you bundle the correlated information together, it is easier to determine where you got the information and what it is describing.

Also think about future development for the project, for example adding another descriptor for the user like aux3. For the first example you would need to add another hashmap, remember to add to the new hashmap, and remember to pull data from it as you are pulling from the others. This would create a lot of boilerplate for initial set up and access.

uuid = //some uuid
Map&lt;UUID, Double&gt; aux3 = new HashMap&lt;UUID, Double&gt;();

// some code
aux3.put(uuid, 1.0);

// some code ...
aux3.get();

But with composition method, you'd only need to remember to add the class property (with successors and mutators), and you are free to use these anywhere. Anytime you access from the user map, you only need I call to get(uuid) and you have access to all the associated data.

答案2

得分: 0

这是一个主观话题 - 评估以下因素

  1. 信息安全(仅因为您提到了用户) - 您是否能够将所有信息保持在一起
  2. 取决于您对这些地图进行了什么操作,以及后续处理可能会出现问题的可能性

否则,通常第二个选项(UserInfo类)是一个不错的主意。

英文:

This is Subjective topic - evaluate below factors

  1. Information Security (just because you mentioned user) - can you keep all the information together
  2. depends on what you doing with these Maps and if there are likely to be issues because of later processing

otherwise, generally 2nd option (UserInfo class) is a good to idea.

huangapple
  • 本文由 发表于 2020年8月25日 11:30:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63571653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定