以下是一些代码:
ENJOY08A,
AUTO09B,
PLAY06D,
SUMMER08W,
WINTER03S,
LEAF02A,
这些值中的每一个都对应于一个特定区域。
例如ENJOY08A
和AUTO09B
对应DEPT_A
PLAY06D
对应DEPT_B
SUMMER08W
、WINTER03S
和LEAF02A
对应DEPT_C
有固定数量的区域(5 个区域),但代码不受限制。一个代码仅对应一个区域,但一个区域可以有任意数量的代码。
以上均不会存储在数据库中。
我需要创建一个支持以下操作的 Java 类。
给定代码,我需要知道相应的区域。
给定区域,我需要知道所有相应的代码。
设计 Java 类的最佳方法是什么?
The following are some of the codes:
ENJOY08A,
AUTO09B,
PLAY06D,
SUMMER08W,
WINTER03S,
LEAF02A,
Each of these values correspond to a specific area.
For example, ENJOY08A
and AUTO09B
correspond to DEPT_A
PLAY06D
corresponds to DEPT_B
SUMMER08W
, WINTER03S
and LEAF02A
corresponds to DEPT_C
There are a fixed number of areas (5 areas), but unlimited codes. A code will correspond to only one area, but an area can have any number of codes.
None of the above will be stored in the database.
I need to create a Java class which will support the following operations..
Given the code, I need to know the corresponding area.
Given the area, I need to know all the corresponding codes.
What's the best way to go about designing the Java class?
发布评论
评论(5)
查看 guava multimap。我相信它提供了您想要的功能。
Check out the guava multimap. I believe it provides the functionality you want.
我认为最简单的方法是将AREAS放入
HashMap中,然后将所有关联的CODES输入到相关映射中。然后,您可以获得某个区域的所有代码的集合,或者迭代这些集合以查找哪一个包含您正在搜索的代码。I think the simplest method is to put the AREAS into a
<String, Set>
HashMap, then enter all the associated CODES into the relevant map. Then, you can get the Set of all codes for an area or iterate over the sets to find which one contains the code you are searching for.MultiMap
(Google guava 库)(一种双向地图)作为后端。Properties
类从属性文件中填充regionForCode(String code)
和codeForRegion (字符串区域)
,它使用 BiMap 来检索映射的值。您甚至可以考虑将区域放入
enum
而不是简单的String
中,因为您的区域值是固定的。那么这个领域的描述就会更加一致。编辑:我注意到 BiMap 用于独特的映射。 MultiMap 的 anser 是正确的,所以我更正了我的答案。
MultiMap
(Google guava library), which is a bi-directional map, as a backend.Properties
classregionForCode(String code)
andcodeForRegion(String region)
which use the BiMap to retrieve the mapped values.You might even consider putting region into an
enum
instead of a simpleString
, because your region values are fixed. Then the domain would be described a little bit more consistently.Edited: I noticed that BiMap is for unique mappings. The anser with MultiMap is correct, so I corrected my answer.
由于代码的数量是无限的,因此您需要制定一个将代码映射到部门的规则。可能类似于将所有代码相加,然后对总和取模 5。这个规则有无数种选择。
不过
,我认为“代码”的概念(与压缩或加密相反)是代码值是固定的。任何基于规则的解决方案都可以更容易地找出。
Since the number of codes is unlimited, you need to come up with a rule for mapping codes into departments. Probably it will be something like add up all the codes and take that sum modulo 5. There are an infinite number of choices for what this rule can be.
Something like
I think, though, that the notion of "code" (as opposed to compression or encryption) is that the code values are fixed. Any rule-based solution can be figured out more easily.
我会使用一个带有两个
Map
的类。第一个是
Map>
,用于查找给定区域的代码集(或者List
)。有人还建议使用 Guava 的 MultiMap,本质上就是这样。第二个是
Map
,用于查找任何给定代码的区域。然后只需实现适当的
String findAreaForCode(String code)
和Set; listCodesInArea(String area)
方法。最后,如果区域列表很小、有限且相对静态(不需要在运行时动态增长),那么我会考虑使用 Java
enum
代替普通的字符串
。I would use a class with two
Maps
.The first is a
Map<String, Set<String>>
for looking up the set (alternativelyList
) of codes for a given area. Somebody also suggested Guava'sMultiMap
, that is essentially what this is.The second would be a
Map<String, String>
for looking up the area of any given code.Then just implement the appropriate
String findAreaForCode(String code)
andSet<String> listCodesInArea(String area)
methods.Finally, if the list of areas is small, finite and relatively static (doesn't need to grow dynamically at run-time) then I would consider using a Java
enum
in place of an ordinaryString
.