Fastest way to check if a byte array is all zeros

前端 未结 5 767
栀梦
栀梦 2021-02-03 18:06

I have a byte[4096] and was wondering what the fastest way is to check if all values are zero?

Is there any way faster than doing:

byte[] b          


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-03 18:59

    For Java 8, you can simply use this:

    public static boolean isEmpty(final byte[] data){
        return IntStream.range(0, data.length).parallel().allMatch(i -> data[i] == 0);
    }
    

提交回复
热议问题