提高将大量排序映射合并为一个排序映射的性能 - java

发布于 2024-08-14 22:55:35 字数 433 浏览 5 评论 0原文

我有一种获取 SortedMap 作为输入的方法,该映射包含许多 SortedMap 对象,该方法的输出应该是一个包含输入映射中保存的映射的​​所有元素的 SortedMap。该方法如下所示:

private SortedMap mergeSamples(SortedMap map){
  SortedMap mergedMap = new TreeMap();
  Iterator sampleIt = map.values().iterator();
  while(sampleIt.hasNext())
  {
    SortedMap currMap = (SortedMap) sampleIt.next();
    mergedMap.putAll(currMap);
  }
  return mergedMap;
}

这是一个性能杀手,我可以在这里改进什么?

I have a method that gets a SortedMap as input, this map holds many SortedMap objects, the output of this method should be one SortedMap containing all elements of the maps held in the input map. the method looks like this:

private SortedMap mergeSamples(SortedMap map){
  SortedMap mergedMap = new TreeMap();
  Iterator sampleIt = map.values().iterator();
  while(sampleIt.hasNext())
  {
    SortedMap currMap = (SortedMap) sampleIt.next();
    mergedMap.putAll(currMap);
  }
  return mergedMap;
}

This is a performance killer, what can I improve here?

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

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

发布评论

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

评论(3

萝莉病 2024-08-21 22:55:35

我没有发现你的代码有什么问题;您真正能做的就是尝试 SortedMap 的替代实现。第一个是 ConcurrentSkipListMap 和然后查看 Commons CollectionsGoogle 收藏GNU Trove 。后者可以产生非常好的结果,特别是当映射的键和值是原始类型时。

I don't see anything wrong with your code; all you can really do is try alternative implementations of SortedMap. First one would be ConcurrentSkipListMap and then look at Commons Collections, Google Collections and GNU Trove. The latter can yield very good results especially if your maps' keys and values are primitive types.

画中仙 2024-08-21 22:55:35

输入是否需要为 SortedMap?对我来说,如果输入只是一个集合或列表,这似乎会更容易。这可能会加快创建输入的速度,并且可能会使所有包含的地图的迭代速度更快。

除此之外,我认为提高此代码性能的最可能来源是提高正在合并的排序映射中的值的compareTo() 实现的速度。

Is it a requirement for the input to be a SortedMap? To me it would seem easier if the input was just a Collection or List. That might speed up creating the input, and might make iteration over all contained maps faster.

Other than that I believe the most likely source of improving the performance of this code is by improving the speed of the compareTo() implementation of the values in the the sorted maps being merged.

恍梦境° 2024-08-21 22:55:35

你的代码已经尽善尽美了。但是,在我看来,数据结构的整体设计需要进行一些彻底修改:您正在使用 SortedMap,但未使用父映射的键。

你想用它来表达一棵带有嵌套元素的树,而你的任务是压平树吗?如果是这样,请创建一个支持您的方法的 Tree 类,或者使用智能方式来合并键:

public class NestedKey implements Comparable<NestedKey> {

  private Comparable[] entries;

  public NestedKey(Comparable... entries) {
    assert entries != null;
    this.entries = entries;
  }

  public int compareTo(NestedKey other) {
    for(int i = 0; i < other.entries.length; i++) {
      if (i == entries.length)
        return -1; // other is longer then self <=> self is smaller than other
      int cmp = entries[i].compareTo(other.entries[i]);
      if (cmp != 0)
        return cmp;
    }
    if (entries.length > other.entries.length)
      return 1; // self is longer than others <=> self is larger than other
    else
      return 0;
  }

}

用作 SortedMap 的键的 NestedKey 条目与其他 NestedKey 条目进行比较> 通过比较其每个条目来对象。存在于所有元素中但具有更多条目的 NestedKey 被假定为更大。因此,您有这样的关系:

  • NestedKey(1, 2, 3) < NestedKey(1, 2, 4)
  • NestedKey(1, 3, 3) < NestedKey(2, 1, 1)
  • NestedKey(1, 2, 3) < NestedKey(2)

如果您仅使用一个使用 NestedKey 作为其键的 SortedMap,则其 .values() 集会自动返回所有条目(展平)。但是,如果您只想使用 SortedMap 的部分内容,则必须使用 .subMap。例如,如果您希望所有条目都包含 2 到 3 之间的 NestedKey,请使用 .subMap(new NestedKey(2), new NestedKey(3))

Your code is as good as it gets. However, it seems to me that the overall design of the data structure needs some overhaul: You are using SortedMap<?, SortedMap<?, ?>, yet the keys of the parent map are not used.

Do you want to express a tree with nested elements with that and your task is it to flatten the tree? If so, either create a Tree class that supports your approach, or use an intelligent way to merge the keys:

public class NestedKey implements Comparable<NestedKey> {

  private Comparable[] entries;

  public NestedKey(Comparable... entries) {
    assert entries != null;
    this.entries = entries;
  }

  public int compareTo(NestedKey other) {
    for(int i = 0; i < other.entries.length; i++) {
      if (i == entries.length)
        return -1; // other is longer then self <=> self is smaller than other
      int cmp = entries[i].compareTo(other.entries[i]);
      if (cmp != 0)
        return cmp;
    }
    if (entries.length > other.entries.length)
      return 1; // self is longer than others <=> self is larger than other
    else
      return 0;
  }

}

The NestedKey entry used as a key for a SortedMap compares to other NestedKey objects by comparing each of its entries. NestedKeys that are in all elements present, but that have more entries are assumed to be larger. Thus, you have a relationship like this:

  • NestedKey(1, 2, 3) < NestedKey(1, 2, 4)
  • NestedKey(1, 3, 3) < NestedKey(2, 1, 1)
  • NestedKey(1, 2, 3) < NestedKey(2)

If you use only one SortedMap that uses NestedKey as its keys, then its .values() set automatically returns all entries, flattened out. However, if you want to use only parts of the SortedMap, then you must use .subMap. For example, if you want all entries wite NestedKeys between 2 and 3 , use .subMap(new NestedKey(2), new NestedKey(3))

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