在Java中使用多级HashMap的最佳实践 [英] Best Practices for Using Multi Level HashMap in Java

查看:705
本文介绍了在Java中使用多级HashMap的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一种情况,即使用多级哈希映射结束;也就是说,哈希映射内部的散列映射深度达到三到四层。

本能地,这在某处感觉不对。我已经在这里阅读过关于如何迭代/使用多级哈希映射的帖子,但几乎没有人说这是最佳实践。



为什么是多级哈希映射不好,如果有更好的设计会怎样?

以下是我们有多级哈希映射的示例设计: p>

 映射< String,Object1> MAP1; 

类Object1 {
字符串版本;
地图< String,Object2> MAP2;
}

class Object2 {
Map< String,List< Object3>> MAP4;
Map< String,String> MAP5;


解决方案

只要他们正确地抽象出来,这不是很重要的一点,但是你可以从可读性的角度来看待一些令人讨厌的兔子洞。如果没有抽象的话,保持这一点就会变成一个 噩梦 ,开发人员不会希望在另一个上。重新创建是一个种类表;第一个关键是获得更多列的主键。在简单的一个,两个或三个层次的设计中,这并不可怕;你需要三个键来获得单个值。假设有一个方便的方法来访问它,就像下面这样,它不是一个可怕的想法(尽管有更好的办法)。

  public interface Table< K1,K2,K3,V> {
V get(K1 key1,K2 key2,K3 key3);
}

...然而,这一切都取决于你实际上是什么>使用该数据结构做。如果您发现自己试图迭代中间键的值(也就是说,您正在查看Key 3以获取它与Key 5之间的所有值的集合),那么您必须在此时重新考虑您的业务逻辑。所提供的数据结构不够灵活,无法处理所有 案件;或多或少,它用于基于一组值的简单索引。



或者,您可以查看 Guava ,因为它具有相同的功能,有一个更好的接口(就像我上面的那个接口)。


We have a situation where we are ending up using multi-level hash maps; that is, a hash map inside of a hash map, three or four levels deep.

Instinctively this felt wrong somewhere. I have read posts here that talks about how to iterate/use a multi-level hash map, but hardly any of them say what is the best practice for this.

Why are multi level hash maps bad, and what would be the better design, if any?

Here is the sample design of multi-level hash maps that we have:

Map<String, Object1> map1;

class Object1 {
    String version;
    Map<String,Object2> map2;
}

class Object2 {
    Map<String,List<Object3>> map4;
    Map<String,String> map5;
}

解决方案

As long as they're properly abstracted, it's not that big of a deal, but you lead yourself down some nasty rabbit holes in terms of readability. Without abstraction, maintaining this becomes a nightmare that no developer would wish on another.

Essentially, what you're creating is a table of sorts; the first key is the primary key to gain access to further columns. On a simple one, two, or three-level design, this isn't terrible; you need three keys to get a single value. Provided that there's a convenient way to access it, like the below, it's not a terrible idea (although there are far better out there).

public interface Table<K1, K2, K3, V> {
    V get(K1 key1, K2 key2, K3 key3);
}

...However, it all depends on what you're actually doing with that data structure. If you find yourself attempting to iterate intermediate keys for values (that is, you're looking at Key 3 for the collection of all values between it and Key 5), you've got to rethink your business logic at that point. The data structure provided isn't flexible enough to handle all cases; more or less, it's used for simplistic indexing based on a set of values.

Alternatively, one could look into a Guava Table, as that does the same sort of thing, with a better interface to it (something like the one I have above).

这篇关于在Java中使用多级HashMap的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆