繁体   English   中英

使用什么数据结构?

[英]What data structure to use?

我需要存储以下数据;

Clampls = {"23e23e", "ff333g", "fhgswq"," h65h3", "ffwwf", "34rf3"}
KJAS3.2 = {"f34f4f", "43rf2d", "3rfas1"," 1122d", "fff42", "ff33f"}
...

我本来想存储这样的东西

 Name        Tokens
  .       -> ... , ... , ... , ...
  .       -> ... , ... , ... , ...
Clampls   -> "23e23e" , "ff333g" , "fhgswq" , ... 
KJAS3.2   -> "f34f4f" , "43rf2d" , "3rfas1" , ...  
  .       -> ... , ... , ... , ...
  .       -> ... , ... , ... , ...

所以有点像HashMap。 在R. Lafore的《 Java中的数据结构与算法》一书中做了一些阅读,发现我需要的是Separate Chaining / HashChain但是他们使用自己构建的数据结构类对其进行了解释。

我可以在Java中使用HashChain的“现成”集合吗? 就像是

Map<String, []String> theMap = new HashMap<String, []String>(); //just an example
Map<String, List<String>> dataStructure = new HashMap<String, List<String>>();

dataStructure.put("Clampls", Arrays.asList("23e23e", "ff333g", "fhgswq"," h65h3", "ffwwf", "34rf3"));
dataStructure.put("KJAS3.2", Arrays.asList("f34f4f", "43rf2d", "3rfas1"," 1122d", "fff42", "ff33f"));

dataStructure.put("KJAS3.3", new ArrayList<String>());
dataStructure.get("KJAS3.3").add("fhgswq");

您的问题不是很清楚,因此该答案可能不正确。 如果要给定键有多个值,则应使用MultiMap。 JDK中没有实现,但是在公共库中有很多实现,例如Google Guava(包含以前的Google Collections)。

诸如Map<String,List<String>>类的替代方法可行,但使用起来很麻烦,例如,如果键不存在,则必须自己创建“内部”列表。

暂无
暂无

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

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