1

我正在将两个ArrayLists 与以下代码合并。该代码正在运行并给我想要的结果,但我想要一个更高效的版本。以下是条件。

  1. 方法接受两个列表,并且两个列表的元素都按降序排列 (5,4,3,2)
  2. 方法接受一个整数来决定结果的大小ArrayList
  3. 第一个输入列表大小永远不会大于结果的大小ArrayList

代码:

public ArrayList<Integer> mergeList(ArrayList<Integer> first,ArrayList<Integer> second, int n){
    //case 1: when both list are null.
    if(first == null && second == null )
        return null;
    //case 2: when first list is null but second list have elements
    else if( first == null && second != null){
        return second.size() >=n ? new ArrayList<Integer>(second.subList(0, n)) : second;
    }
    //case 3: when first list have record and second list is null
    else if(first != null && second == null){
        return first;
    }
    //case 4: when both list have elements 
    else {
        first.addAll(second);
        Collections.sort(first);
        Collections.reverse(first);
        return first.size()>=n ? new ArrayList<Integer>(first.subList(0, n)) : first;
    }
}

}

4

2 回答 2

1

这取决于您所说的“更高效”是什么意思。

在什么方面?内存、CPU、可读性?

根据您上面的代码,我做出以下假设:

  • 可读性比没有任何分析测量/要求的纯性能/内存消耗更重要“程序优化的第一条规则:不要这样做。程序优化的第二条规则(仅限专家!):不要这样做。 " ——迈克尔·杰克逊
  • 首选 null 对象模式而不是返回 null
  • 重复元素是可取的/必需的
  • 使用比较器执行反向排序

private List<Integer> mergeList(List<Integer> list1, List<Integer> list2, final int newSize) {

    // Enforce null object pattern
    if (list1 == null) {
        list1 = Collections.emptyList();
    }
    if (list2 == null) {
        list2 = Collections.emptyList();
    }

    // If duplicates are not desirable, a TreeSet would perform automatic sorting.
    List<Integer> result = new ArrayList<Integer>(list1);
    result.addAll(list2);

    Comparator<Integer> reverseSortComparator = new Comparator<Integer>() {

        @Override
        public int compare(final Integer o1, final Integer o2) {
            return o2.compareTo(o1);
        }
    };

    Collections.sort(result, reverseSortComparator);

    if (result.size() > newSize) {
        return result.subList(0, newSize);
    } else {
        return result;
    }
}
于 2013-09-25T04:01:34.320 回答
0

看起来您正在尝试保留firstand的内容second。如果你不是,那么这对你来说很好,并且会让你的代码更快、更易读:

public ArrayList<Integer> mergeList(ArrayList<Integer> first,ArrayList<Integer> second, int maxLength){

    //case 1: when both list are null.
    if(first == null && second == null )
        return null;
    //case 2: when first list is null but second list have elements
    else if( first == null && second != null){
        return second;
    }
    //case 3: when first list have record and second list is null
    else if(first != null && second == null){
        return first;
    }
    //case 4: when both list have elements 
    else if(first != null && second != null){
        first.addAll(second);
        Collections.sort(first); //want to merge these two line into one
        Collections.reverse(first);
    }
    return (ArrayList) first.size() > maxLength ? first.subList(0, n) : first;
}

这更快的原因是因为对于每个addAll(),Java 必须遍历所有项目,将它们复制到tempList. 我保留了Collections.reverse通话,因为您似乎需要以反向排序顺序保存数据。

于 2013-09-25T03:38:20.710 回答