javaarrayslambdaduplicateslang

Remove a duplicated array element but getting some error


When the code flow gets to the removeIf() logic the code breaks and it doesn't return anything as if it was a lang error. Please use the same code logic if possible.

public static SalmonSumario[] filterSummario(SalmonSumario[] salmon){
  Set<String> filter = new TreeSet<>();
  
  List<SalmonSumario> salmonSum = Arrays.asList(salmon);
  
  salmonSum.removeIf(array -> !filter.add(array.getSalmonNumber()));
  
  return salmonSum.toArray(new SalmonSumario[0]);
}

Solution

  • The list returned by Arrays.asList() has a fixed length so you can't add nor remove elements, Therefore, you will have to wrap it with new ArrayList<>(Arrays.asList(salmon))