简体   繁体   English

在值中存储TreeMap的键(java)

[英]Storing Key of TreeMap in Value (java)

TreeMap s are instantiated by Key, Value pairs. TreeMapKey, Value对实例化。 For me, the Key is an IP address, and the Value s are objects that contain stats about that IP address. 对我来说, Key是一个IP地址, Value是包含有关该IP地址的统计信息的对象。

Is there ever a reason to redundantly store the the Key inside the Value object? 是否有过一个理由冗余存储的Key里面Value目标? I'm tempted to save space and leave it out, but intuitively it feels wrong not to have the Key inside the object (for good encapsulation). 我很想节省空间并将其遗漏,但直观地认为没有Key在对象内部(为了良好的封装)是错误的。

请记住,包含密钥只会将值对象的大小增加32位或64位(取决于您使用的是32位还是64位JVM),因此决定是否包含它可能不会对你的程序的内存消耗产生重大影响 - 因此我会错误地包含它而不是不包括它。

Your design is flawed - you're in "object denial". 你的设计存在缺陷 - 你处于“拒绝对象”状态。

You should create a class to hold the details, including the IP address. 您应该创建一个类来保存详细信息,包括IP地址。

If you then want fast retrieval of the details given an IP address, you could store them in a Map using the IP address as the key. 如果您希望快速检索给定IP地址的详细信息,则可以使用IP地址作为密钥将它们存储在Map中。

In a way, this is what you're asking, because the key being used in the map is stored in the value object. 在某种程度上,这就是你所要求的,因为地图中使用的密钥存储在值对象中。 It's just that with the proper design, the key is already part of the object stored in the value. 只是通过适当的设计,密钥已经是存储在值中的对象的一部分。

Is there ever a reason to redundantly store the the Key inside the Value object? 有没有理由在Key对象中冗余地存储Key?

First of all, you wouldn't be storing the key "inside" the value object. 首先,您不会将值存储在值对象“内部”。 Rather, the value object would have a reference to the key object. 相反,值对象将具有对关键对象的引用。 So you are not storing a redundant copy at all. 因此,您根本不存储冗余副本。 (Or at least, not unless you are deliberately copying / cloning the key objects ... which would be rather silly.) (或者至少,除非你故意复制/克隆关键对象......这将是相当愚蠢的。)

The only question is whether the reference to the key in the value object is "redundant". 唯一的问题是对值对象中的键的引用是否是“冗余的”。 In a sense it is, but the flip-side is that redundancy is likely to simplify your code ... and potentially make it more efficient in other places. 从某种意义上说,它是,但另一方面,冗余可能会简化您的代码......并可能使其在其他地方更有效。 We can't make a judgement on that without looking at how these objects are used (elsewhere) in your code. 我们无法在不查看代码中如何使用这些对象(其他地方)的情况下做出判断。 But the chances are that it doesn't make much difference anyway; 但无论如何它的可能性并没有多大差别; see below. 见下文。

I'm tempted to save space and leave it out, but intuitively it feels wrong not to have the Key inside the object (for good encapsulation). 我很想节省空间并将其遗漏,但直观地认为没有Key在对象内部(为了良好的封装)是错误的。

The space saving in not having a reference to the key in the value is one word (32 or 64 bit) per value. 没有引用值中的键的空间节省是每个值一个字(32或64位)。 This is most likely too small to worry about, even if you have millions of these objects. 即使您有数百万这些对象,这也很可能太小而无法担心。 The reality is that the one extra word it is likely to be less than 5% of the overall per-object space usage for a given key + value + map entry. 实际情况是,对于给定的键+值+映射条目,一个额外的单词可能小于整个每对象空间使用的5% That 5% is unlikely to make a significant difference ... 这5%不太可能产生重大影响......

The issue of encapsulation is context dependent. 封装问题取决于上下文。 It really depends where the encapsulation boundaries are, and how porous they can be. 这实际上取决于封装边界的位置,以及它们的多孔性。 Encapsulation isn't an end goal. 封装不是最终目标。 It is one of a set of "tools" that can be used in designing / implementing maintainable software. 它是可用于设计/实现可维护软件的一组“工具”之一。 It is not always the right tool, and it certainly has to be used appropriately; 它并不总是正确的工具,它必须得到适当的使用; ie with a clear understanding of how it helps (or hinders) with the problem at hand. 即清楚地了解它如何帮助(或阻碍)手头的问题。

But here's an idea: you could make the "value" objects both the key and the value in the Map ... and use a Comparator . 但是这里有一个想法:你可以将“值”对象作为Map的键和值...并使用Comparator Depending on the details, that would allow you to hide the IP address (etc) entirely within the encapsulation boundary. 根据细节,这将允许您完全隐藏封装边界内的IP地址(等)。 (That approach won't work with a HashMap though ...) (虽然这种方法不适用于HashMap ...)


Final piece of advice. 最后一条建议。

You seem to be doing what experienced developers would recognize as "premature optimization". 您似乎正在做有经验的开发人员认为“过早优化”的事情。 This is usually a bad idea. 这通常是一个坏主意。 My advice is to stop worrying about the space usage for now. 我的建议是暂时不要担心空间使用情况。

  • Wait until you've got the application working, and you are in a position to benchmark with real input data. 等到应用程序正常运行,您就可以对实际输入数据进行基准测试。

  • Benchmark and see if the performance (ie memory usage) is acceptable. 基准测试并查看性能(即内存使用情况)是否可接受。

  • If (and only if) it is unacceptable, profile the application to look for opportunities to reduce memory usage. 如果(并且仅当)它是不可接受的,请分析应用程序以寻找减少内存使用的机会。

  • Implement and rerun the benchmark to see if it has made a difference. 实施并重新运行基准,看它是否有所作为。

  • Repeat as required. 根据需要重复。

如果密钥将在​​应用程序中的任何其他位置使用(如果是值对象),那么将其保留在对象内是个好主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM