具有非唯一键的排序映射

发布于 2024-09-30 09:49:12 字数 319 浏览 3 评论 0 原文

时使用什么结构

  1. 当需要按键对元素进行排序
  2. 能够保存非唯一键

    结构<整数、字符串> struct = new Structure<整数、字符串>;
    struct.add(3,"...");
    struct.add(1,"约翰");
    struct.add(2,"埃德温");
    struct.add(1,"玛丽");
    struct.toString() == {key->;值;} [1->“约翰”,1->“玛丽”,2->“埃德温”,3->“...”]
    

What structure to use when one needs

  1. ordering of elements by key
  2. ability to hold non-unique keys

    Structure<Integer, String> struct = new Structure<Integer, String>;
    struct.add(3,"...");
    struct.add(1,"John");
    struct.add(2,"Edwin");
    struct.add(1,"Mary");
    struct.toString() == {key-> value;} [1->"John",1->"Mary",2->"Edwin",3->"..."]
    

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

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

发布评论

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

评论(3

嗳卜坏 2024-10-07 09:49:12

如果您想使用标准 Java API,我会选择 TreeMap>

  • 元素按键排序,因为它是一个 SortedMap。来自文档:

    <块引用>

    映射根据其键的自然顺序进行排序,或者通过通常在排序映射创建时提供的比较器进行排序。当迭代排序映射的集合视图(由entrySet、keySet 和values 方法返回)时,会反映此顺序。

  • 该结构允许使用非唯一的键,因为您可以让一个键映射到多个对象。

这种类型的结构称为排序多重映射,并且有几种实现可以隐藏首次插入时创建初始集的详细信息等。请查看GuavaApache例如,Commons

根据您的需要,您还可以有一个 SortedSet>,其中元素按对中左侧元素排序。 (请注意,您可以自己编写 Pair 类,但它不应超过几行。)

If you want use the standard Java API, I would choose a TreeMap<Integer, Set<String>>.

  • The elements are ordered by the keys since it is a SortedMap. From the docs:

    The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods).

  • The structure allows for non-unique keys as you can let one key map to multiple objects.

This type of structure is called a sorted multi-map and there are several implementations that hide the details of creating initial sets when inserting for the first time etc. Have a look at Guava or Apache Commons for instance.

Depending on your needs, you could also have a SortedSet<Pair<Integer, String>>, where the elements are sorted on the left element in the pair. (Note that you would have write the Pair class yourself, but it shouldn't be more than a few lines.)

如梦 2024-10-07 09:49:12

听起来您可能需要一个 Map> ,以便每个键映射到字符串列表(或其他集合)。

Apache Commons 有一个 MultiMap,无需编写代码即可完成上述操作。

 MultiMap mhm = new MultiHashMap();
 mhm.put(key, "A");
 mhm.put(key, "B");
 mhm.put(key, "C");
 Collection coll = (Collection) mhm.get(key);

coll 将是包含“A”、“B”、“C”的集合。

我怀疑 Google Collections 也会提供类似的东西。

It sounds like you need perhaps a Map<Integer, List<String>> so each key maps to a list (or other collection) of strings.

Apache Commons has a MultiMap, which does the above without the hassle of you coding it.

 MultiMap mhm = new MultiHashMap();
 mhm.put(key, "A");
 mhm.put(key, "B");
 mhm.put(key, "C");
 Collection coll = (Collection) mhm.get(key);

coll will be a collection containing "A", "B", "C".

Google Collections will provide something similar, I suspect.

拥抱我好吗 2024-10-07 09:49:12

除了使用 Apache Commons 或 Guava 中的 multimap 实现,或者按照其他答案的建议实现 Pair 类之外,您还可以简单地使用 TreeMap>。它现在不再是映射到单个 String 的键,而是映射到可以保存多个值的 List,从而像多重映射一样有效地工作。

但我会为生产代码选择合适的多重映射。

Apart from using multimap implementations from Apache Commons or Guava, or implementing a Pair class as suggested by other answers, you can simply use a TreeMap<Integer,List<String>>. Instead of a key mapping to a single String, it now maps to a List which can hold multiple values and thus work effectively like a multimap.

But I'd go with a proper multimap for production code though.

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