Java类设计支持多值选项

发布于 2024-11-28 03:20:50 字数 519 浏览 0 评论 0 原文

以下是一些代码:

ENJOY08A,
AUTO09B,
PLAY06D,
SUMMER08W,
WINTER03S,
LEAF02A,

这些值中的每一个都对应于一个特定区域。 例如ENJOY08AAUTO09B对应DEPT_A

PLAY06D对应DEPT_B

SUMMER08WWINTER03SLEAF02A对应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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放手` 2024-12-05 03:20:50

Check out the guava multimap. I believe it provides the functionality you want.

抚笙 2024-12-05 03:20:50

我认为最简单的方法是将AREAS放入 HashMap中,然后将所有关联的CODES输入到相关映射中。然后,您可以获得某个区域的所有代码的集合,或者迭代这些集合以查找哪一个包含您正在搜索的代码。

for (String k : (Set<String>)areas.keySet()) {
 if (areas.get(k).contains(theCode))
  return k;
}
return "NO CORRESPONDING AREA FOUND";

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.

for (String k : (Set<String>)areas.keySet()) {
 if (areas.get(k).contains(theCode))
  return k;
}
return "NO CORRESPONDING AREA FOUND";
怀中猫帐中妖 2024-12-05 03:20:50
  1. 使用 MultiMap(Google guava 库)(一种双向地图)作为后端。
  2. 使用普通的 put 填充地图,无论是在构造期间还是使用 Java Properties 类从属性文件中填充
  3. 提供两个接口方法:regionForCode(String code)codeForRegion (字符串区域),它使用 BiMap 来检索映射的值。

您甚至可以考虑将区域放入enum而不是简单的String中,因为您的区域值是固定的。那么这个领域的描述就会更加一致。

编辑:我注意到 BiMap 用于独特的映射。 MultiMap 的 anser 是正确的,所以我更正了我的答案。

  1. Use a MultiMap (Google guava library), which is a bi-directional map, as a backend.
  2. Fill the map using normal puts, either during construction time or from a properties file using the Java Properties class
  3. Offer two interface methods: regionForCode(String code) and codeForRegion(String region) which use the BiMap to retrieve the mapped values.

You might even consider putting region into an enum instead of a simple String, 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.

情愿 2024-12-05 03:20:50

由于代码的数量是无限的,因此您需要制定一个将代码映射到部门的规则。可能类似于将所有代码相加,然后对总和取模 5。这个规则有无数种选择。

不过

public class DepartmentCoder {

    public static String toCode(Department department) {
        // TODO: Randomly generate a string having the desired property, such
        // as the sum of the string's codepoint modulo the number of departments
        // equaling the ordinal value of the department.       
    }

    public static Department(String code) {
        // TODO: Do some math on the codepoints of the string
        return result % Deparatments.NUMBER_OF_DEPARTMENTS;
    }
}

,我认为“代码”的概念(与压缩或加密相反)是代码值是固定的。任何基于规则的解决方案都可以更容易地找出。

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

public class DepartmentCoder {

    public static String toCode(Department department) {
        // TODO: Randomly generate a string having the desired property, such
        // as the sum of the string's codepoint modulo the number of departments
        // equaling the ordinal value of the department.       
    }

    public static Department(String code) {
        // TODO: Do some math on the codepoints of the string
        return result % Deparatments.NUMBER_OF_DEPARTMENTS;
    }
}

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.

梦初启 2024-12-05 03:20:50

我会使用一个带有两个 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 (alternatively List) of codes for a given area. Somebody also suggested Guava's MultiMap, 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) and Set<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 ordinary String.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文