代码如下：

```
public static int longestConsecutive(int[] nums) {
if (nums == null || nums.length == 0){
return 0;
}
Arrays.sort(nums);
int max = 1;
int temp = 1;
for (int i = 0; i < nums.length-1; i++) {
if (nums[i+1] - nums[i] == 1){
temp++;
}else if (nums[i+1] - nums[i] == 0){
continue;
}else {
max = Math.max(max,temp);
temp=1;
}
}
max = Math.max(max,temp);
return max;
}
```