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

提高将大量已排序映射合并为一个已排序映射的性能-java,java,performance,merge,sortedmap,Java,Performance,Merge,Sortedmap,我有一个方法,它获取一个SortedMap作为输入,这个map保存了很多SortedMap对象,这个方法的输出应该是一个SortedMap,包含输入map中保存的map的所有元素。方法如下所示: private SortedMap mergeSamples(SortedMap map){ SortedMap mergedMap = new TreeMap(); Iterator sampleIt = map.values().iterator(); while(sampleIt.ha

我有一个方法,它获取一个SortedMap作为输入,这个map保存了很多SortedMap对象,这个方法的输出应该是一个SortedMap,包含输入map中保存的map的所有元素。方法如下所示:

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;
}

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

我看不出您的代码有任何问题;您真正能做的就是尝试
SortedMap
的替代实现。第一个是,然后看,然后。后者可以产生非常好的结果,尤其是当您的地图的键和值是基本类型时;您真正能做的就是尝试
SortedMap
的替代实现。第一个是,然后看,然后。后者可以产生非常好的结果,尤其是当地图的键和值是基本类型时。

是否要求输入为SortedMap?对我来说,如果输入仅仅是一个集合或列表,那就更容易了。这可能会加快创建输入的速度,并且可能会加快对所有包含的映射的迭代


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

是否要求输入为已排序映射?对我来说,如果输入仅仅是一个集合或列表,那就更容易了。这可能会加快创建输入的速度,并且可能会加快对所有包含的映射的迭代


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

您的代码非常好。然而,在我看来,数据结构的总体设计需要一些改进:您使用的是
SortedMap
,但没有使用父映射的键

你想用嵌套元素表达一棵树吗?你的任务是展平这棵树吗?如果是,请创建支持您的方法的树类,或使用智能方式合并键:

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;
  }

}
public类NestedKey实现了可比较的{
私人可比[]分录;
公共嵌套密钥(可比较的…条目){
断言条目!=null;
this.entries=条目;
}
公共整数比较(嵌套键其他){
for(int i=0;iother.entries.length)
return 1;//self比别人长self比别人大
其他的
返回0;
}
}
用作SortedMap键的
NestedKey
项通过比较其每个项与其他
NestedKey
对象进行比较。假定存在于所有元素中但条目较多的嵌套键较大。因此,你有这样的关系:

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;
}
  • NestedKey(1,2,3)
  • NestedKey(1,3,3)
  • NestedKey(1,2,3)

如果只使用一个使用NestedKey作为键的SortedMap,则其
.values()
集将自动返回所有条目,并平展。但是,如果只想使用SortedMap的一部分,则必须使用
.subMap
。例如,如果您希望所有条目的嵌套键都在2和3之间,请使用
.subMap(new NestedKey(2),new NestedKey(3))

您的代码已经非常好了。然而,在我看来,数据结构的总体设计需要一些改进:您使用的是
SortedMap
,但没有使用父映射的键

你想用嵌套元素表达一棵树吗?你的任务是展平这棵树吗?如果是,请创建支持您的方法的树类,或使用智能方式合并键:

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;
  }

}
public类NestedKey实现了可比较的{
私人可比[]分录;
公共嵌套密钥(可比较的…条目){
断言条目!=null;
this.entries=条目;
}
公共整数比较(嵌套键其他){
for(int i=0;iother.entries.length)
return 1;//self比别人长self比别人大
其他的
返回0;
}
}
用作SortedMap键的
NestedKey
项通过比较其每个项与其他
NestedKey
对象进行比较。假定存在于所有元素中但条目较多的嵌套键较大。因此,你有这样的关系:

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;
}
  • NestedKey(1,2,3)
  • NestedKey(1,3,3)
  • NestedKey(1,2,3)

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

我不懂java,但手动循环而不是迭代器,但这只会产生一个小的改进……知道您的映射有多大会很好。对于给定的输入大小,您可能已经有了合理的性能。@buhb-合并映射包含约1200个对象,并且大约有约1000个方法调用。为什么您认为它是性能杀手?我不知道jav