Java 提高合并两个ArrayList的性能

Java 提高合并两个ArrayList的性能,java,data-structures,collections,arraylist,merge,Java,Data Structures,Collections,Arraylist,Merge,我正在将两个ArrayLists与以下代码合并。代码正在工作,并给我期望的结果,但我想要一个更有效的版本。这里是条件 方法接受两个列表,并且两个列表中的元素按降序排列(5,4,3,2) 方法接受一个整数来决定结果ArrayList的大小 第一个输入列表的大小永远不会大于结果ArrayList的大小 代码: public ArrayList合并列表(ArrayList first,ArrayList second,int n){ //案例1:当两个列表都为空时。 if(first==null&&s

我正在将两个
ArrayList
s与以下代码合并。代码正在工作,并给我期望的结果,但我想要一个更有效的版本。这里是条件

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

    public ArrayList合并列表(ArrayList first,ArrayList second,int n){
    //案例1:当两个列表都为空时。
    if(first==null&&second==null)
    返回null;
    //案例2:当第一个列表为空,但第二个列表包含元素时
    else if(first==null&&second!=null){
    返回second.size()>=n?新建ArrayList(second.subList(0,n)):second;
    }
    //案例3:当第一个列表有记录而第二个列表为空时
    else if(第一个!=null&&second==null){
    先返回;
    }
    //案例4:当两个列表都有元素时
    否则{
    第一,addAll(第二);
    集合。排序(第一);
    收藏。反向(第一);
    return first.size()>=n?新建ArrayList(first.subList(0,n)):first;
    }
    }
    

    }

    看起来您正试图保留第一个
    和第二个
    的内容。如果您没有,那么这将对您很好,并将使您的代码更快、更可读:

    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;
    }
    
    公共ArrayList合并列表(ArrayList first,ArrayList second,int maxLength){ //案例1:当两个列表都为空时。 if(first==null&&second==null) 返回null; //案例2:当第一个列表为空,但第二个列表包含元素时 else if(first==null&&second!=null){ 返回第二; } //案例3:当第一个列表有记录而第二个列表为空时 else if(第一个!=null&&second==null){ 先返回; } //案例4:当两个列表都有元素时 else if(第一个!=null&&second!=null){ 第一,addAll(第二); Collections.sort(first);//要将这两行合并为一行吗 收藏。反向(第一); } return(ArrayList)first.size()>maxLength?first.subList(0,n):first; }

    这之所以更快,是因为对于每个
    addAll()
    ,Java都必须迭代所有项,将它们复制到
    templast
    。我保留了
    集合。reverse
    调用是因为您似乎需要将数据按反向顺序排序。

    看起来您正试图保留
    第一个
    第二个
    的内容。如果您没有,那么这将对您很好,并将使您的代码更快、更可读:

    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;
    }
    
    公共ArrayList合并列表(ArrayList first,ArrayList second,int maxLength){ //案例1:当两个列表都为空时。 if(first==null&&second==null) 返回null; //案例2:当第一个列表为空,但第二个列表包含元素时 else if(first==null&&second!=null){ 返回第二; } //案例3:当第一个列表有记录而第二个列表为空时 else if(第一个!=null&&second==null){ 先返回; } //案例4:当两个列表都有元素时 else if(第一个!=null&&second!=null){ 第一,addAll(第二); Collections.sort(first);//要将这两行合并为一行吗 收藏。反向(第一); } return(ArrayList)first.size()>maxLength?first.subList(0,n):first; }

    这之所以更快,是因为对于每个
    addAll()
    ,Java都必须迭代所有项,将它们复制到
    templast
    。我保留了
    集合。reverse
    调用是因为您似乎需要将数据按反向顺序排序。

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

    关于什么?内存、CPU、可读性

    根据上述代码,我做出以下假设:

    • 可读性比没有任何评测测量/需求的纯性能/内存消耗更重要“程序优化的第一条规则:不要这样做。程序优化的第二条规则(仅限专家!):现在不要这样做。”-Michael A.Jackson
    • 与返回null相比,更喜欢null对象模式
    • 需要重复的元素
    • 使用a执行反转 分类

    私有列表合并列表(列表列表1、列表列表2、最终整型新闻大小){
    //强制空对象模式
    if(list1==null){
    list1=Collections.emptyList();
    }
    if(list2==null){
    list2=Collections.emptyList();
    }
    //如果不需要重复项,树集将执行自动排序。
    列表结果=新的ArrayList(列表1);
    结果:addAll(列表2);
    Comparator reverseSortComparator=新比较器(){
    @凌驾
    公共整数比较(最终整数o1,最终整数o2){
    返回o2.compareTo(o1);
    }
    };
    Collections.sort(result,reversesorcomparator);
    if(result.size()>newSize){
    返回结果。子列表(0,新闻大小);
    }否则{
    返回结果;
    }
    }
    
    这取决于你所说的“更高效”是什么意思

    关于什么?内存、CPU、可读性

    根据上述代码,我做出以下假设:

    • 可读性比没有任何评测测量/需求的纯性能/内存消耗更重要“程序优化的第一条规则:不要这样做。程序优化的第二条规则(仅限专家!):现在不要这样做。”-Michael A.Jackson
    • 与返回null相比,更喜欢null对象模式
    • 需要重复的元素
    • 使用a执行反转 分类

    私有列表合并列表(列表li
    
    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;
        }
    }