字节数组ArrayList排序

问题描述 投票:0回答:2

我有一些python代码,可以做以下事情。

for day in server_message.keys():
        for epoch in server_message[day].keys():
            assert sorted(server_message[day][epoch]) == server_message[day][epoch]

我需要用Java来写这些代码 问题是server_message的结构是这样的。

 Map<Integer, Map<Integer, ArrayList<byte[]>>>

如何对ArrayList的字节进行排序?Arrays.sort()和Collections.sort()都没有返回一个新的排序数组,而是在提供的数组上工作。

在Java中有没有什么办法可以解决这个问题,或者我需要自己写一个排序算法来解决这种排序?如何比较两个字节数组?

java sorting
2个回答
1
投票

Array.sort()在后台使用快速算法,我很奇怪为什么Array.sout()不能用,但你可以用快速排序来解决。

public class QuickSort {  
public static void main(String[] args) {  
        int i;  
        int[] arr={90,23,101,45,65,23,67,89,34,23};  
        quickSort(arr, 0, 9);  
        System.out.println("\n The sorted array is: \n");  
        for(i=0;i<10;i++)  
        System.out.println(arr[i]);  
    }  
    public static int partition(int a[], int beg, int end)  
    {  

        int left, right, temp, loc, flag;     
        loc = left = beg;  
        right = end;  
        flag = 0;  
        while(flag != 1)  
        {  
            while((a[loc] <= a[right]) && (loc!=right))  
            right--;  
            if(loc==right)  
            flag =1;  
            elseif(a[loc]>a[right])  
            {  
                temp = a[loc];  
                a[loc] = a[right];  
                a[right] = temp;  
                loc = right;  
            }  
            if(flag!=1)  
            {  
                while((a[loc] >= a[left]) && (loc!=left))  
                left++;  
                if(loc==left)  
                flag =1;  
                elseif(a[loc] <a[left])  
                {  
                    temp = a[loc];  
                    a[loc] = a[left];  
                    a[left] = temp;  
                    loc = left;  
                }  
            }  
        }  
        returnloc;  
    }  
    static void quickSort(int a[], int beg, int end)  
    {  

        int loc;  
        if(beg<end)  
        {  
            loc = partition(a, beg, end);  
            quickSort(a, beg, loc-1);  
            quickSort(a, loc+1, end);  
        }  
    }  
}  

0
投票

我认为你的问题是对一个字节数组进行排序 (byte[]),而不是一个字节数组的列表(List<byte[]>),这没有任何意义。如果你想在不修改现有的字节数组的情况下得到一个排序数组,你可以在.NET中克隆原始数组。

byte[] bytes = {0, 23, 127, -12 };
byte[] clone = bytes.clone();
Arrays.sort(clone);

0
投票

看来你是想对一个成员类型为字节[]的列表进行排序,对吗?

在Java 8+中,你可以使用流来排序,而不触动原始集合。


List<byte [] > list = new ArrayList<>();
        list.add(new byte[]{0x3, 0x4});
        list.add(new byte[]{0x1, 0x2});
        List<byte [] > sortedList = list.stream().sorted(new Comparator<byte[]>() {
            @Override
            public int compare(byte[] o1, byte[] o2) {
                return 0; // Here you should implement the method of comparison of two byte arrays
            }
        }).collect(Collectors.toList());

比较器可以是lambda:

List<byte [] > list = new ArrayList<>();
        list.add(new byte[]{0x3, 0x4});
        list.add(new byte[]{0x1, 0x2});
        List<byte [] > sortedList = list.stream().sorted((o1, o2) -> {
            return 0; // Here you should implement the method of comparison of two byte arrays
        }).collect(Collectors.toList());

这里你需要一个比较器, 因为Java不知道如何比较两个字节数组. 如果它不是一个字节数组(坦率地说,我也不知道用字节数组来做这件事的理由,也不知道这样做是否有意义,但我认为这不是问题的关键),而是一些实现了 java.lang.Comparable 整数、String-s等),那么你就不需要提供一个比较器,只需要使用 sort()

© www.soinside.com 2019 - 2024. All rights reserved.