Like with a List you can type:
public Iterable<Integer> findClosestNumbers(int givenValue, Iterable<Integer> numbers, int n){
Iterable<Integer> closestNumbers;
int closest = numbers.stream()
.min(Comparator.comparingInt(i -> Math.abs(i - value)))
.orElseThrow(() -> new NoSuchElementException("No value present"));
return closestNumbers;
The 'givenValue' represents the value it should be closest to
The 'numbers' could be an array of numbers {1, 2, 4, 5}
The 'n' specifies how many numbers that should be returned closest to the value
Like value:6 and n=2 then results should return Iterable {4, 5}
How to write this using Iterable as short as possible? I can not change the parameters