# How to Find the Closest Integer to a Target Value in a Java List

In this article, we will explore how to find the closest integer to a given target value in a Java list. This is a common task when working with data sets where matching values as closely as possible is important, such as in search algorithms or data analysis.

## 1. Problem Statement

Given:

- A list of integers that may include both positive and negative values.
- A target integer value.

The goal is to find the integer from the list closest to the target value. If two numbers are equally close to the target, the number that appears earlier in the list (i.e., the one with the lower index) should be returned.

For example, consider the following list of integers and target value:

List<Integer> numbers = Arrays.asList(-10, 2, 4, 8, 15); int target = 5;

For the above list and target:

- The number
`4`

is closest to`5`

, with a difference of`1`

. - The number
`8`

is also close to`5`

, with a difference of`3`

. Therefore, the output should be`4`

because it is the closest.

Now, let’s modify the example in the problem statement and demonstrate a scenario where two numbers are equally close to the target value. In such cases, the problem specifies that the number appearing earlier in the list (i.e., the one with the lower index) should be returned.

List<Integer> numbers = Arrays.asList(-10, 2, 4, 6, 8, 15); int target = 5;

In this case:

- The number
`4`

has an absolute difference from the target`5`

of`4 - 5 = 1`

. - The number
`6`

also has an absolute difference of`6 - 5 = 1`

.

Both numbers `4`

and `6`

are equally close to the target, but since `4`

appears earlier in the list (at **index 2**, whereas `6`

is at **index 3**), the result should be `4`

.

## 2. Using an Iterative Method

This approach involves iterating through each element in the list and calculating its absolute difference from the target value. As we go through the list, we keep track of the number with the smallest difference, updating it whenever a closer number is found. If two numbers have the same difference, the number that appears earlier in the list will be returned.

public class ClosestNumberFinder { public static int findClosestNumber(List<Integer> numbers, int target) { int closest = numbers.get(0); int minDiff = Math.abs(numbers.get(0) - target); for (int i = 1; i < numbers.size(); i++) { int currentDiff = Math.abs(numbers.get(i) - target); if (currentDiff < minDiff) { minDiff = currentDiff; closest = numbers.get(i); } } return closest; } public static void main(String[] args) { List<Integer> numbers = List.of(-10, 2, 4, 8, 15); int target = 5; int result = findClosestNumber(numbers, target); System.out.println("Closest number to " + target + " is: " + result); } }

In the `findClosestNumber()`

method, the first step is to initialize the variable `closest`

to the first element in the list, establishing a baseline for comparison. This ensures that the method always has a reference point when checking other numbers.

Next, the code iterates through the list starting from the second element (`i = 1`

). For each number, it calculates the absolute difference between the current number and the target value. This difference is then compared to the smallest difference recorded so far (`minDiff`

). If the current difference is smaller, the current number is updated as the new closest number.

If two numbers have the same difference from the target, the method naturally retains the earlier number as the closest one. This happens because the list is processed sequentially, and the first occurrence of a closest number is not replaced unless a smaller difference is found.

**Output:**

Closest number to 5 is: 4

### 2.1 Edge Cases Example

**Multiple Numbers Equally Close:**The algorithm ensures that in cases where two numbers have equal proximity to the target, the one appearing earlier in the list is returned.**Negative Numbers:**The algorithm works well with both positive and negative numbers, as the difference is calculated using the absolute value.

List<Integer> numbers = List.of(6, -10, 2, 4, 8, 15); int target = 5;

**Output:**

Closest number to 5 is: 6

## 3. Using Java Streams

Using Java Streams, we can use the `min()`

method with a custom comparator to find the closest number in the list.

public class ClosestNumberFinderStream { public static int findClosestNumberStream(List<Integer> numbers, int target) { return numbers.stream() .min(Comparator.comparingInt(num -> Math.abs(num - target))) .orElseThrow(() -> new IllegalArgumentException("List cannot be empty")); } public static void main(String[] args) { List<Integer> numbers = List.of(-10, 2, 4, 8, 15); int target = 5; int result = findClosestNumberStream(numbers, target); System.out.println("Closest number to " + target + " is: " + result); } }

In this example, the `numbers.stream()`

method turns the list into a stream. We then use the `min()`

method, which takes a custom comparator. The comparator compares the absolute difference between each number in the list and the target.

The `orElseThrow()`

method ensures that if the list is empty, an exception is thrown to handle invalid input.

## 4. Using Binary Search (for Sorted Lists)

If the list is **sorted**, we can use binary search to find the closest number. The `Collections.binarySearch()`

method can be used to search for the target, and if the target is not found, it returns the insertion point where the number would be placed in the list. We can then compare the numbers around the insertion point to find the closest value.

public class ClosestNumberFinderBinarySearch { public static int findClosestNumberBinarySearch(List<Integer> numbers, int target) { // Perform binary search int index = Collections.binarySearch(numbers, target); // If exact target is found, return it if (index >= 0) { return numbers.get(index); } // If target is not found, binarySearch returns a negative insertion point (-insertion point - 1) int insertionPoint = -index - 1; // Compare numbers around the insertion point if (insertionPoint == 0) { return numbers.get(0); // Target is smaller than all elements, return the first element } if (insertionPoint == numbers.size()) { return numbers.get(numbers.size() - 1); // Target is larger than all elements, return the last element } // Get the closest of the two neighboring numbers int prev = numbers.get(insertionPoint - 1); int next = numbers.get(insertionPoint); // Return the number that is closer to the target return Math.abs(prev - target) <= Math.abs(next - target) ? prev : next; } public static void main(String[] args) { List<Integer> numbers = List.of(-10, 2, 4, 6, 8, 15); // Example Sorted list int target = 5; int result = findClosestNumberBinarySearch(numbers, target); System.out.println("Closest number to " + target + " is: " + result); } }

The `Collections.binarySearch()`

method returns the index of the target if it is found in the list. If the target is not found, it provides a negative insertion point, indicating where the target would be inserted in the sorted list. After determining the insertion point, we compare the two neighbouring elements around it to find the closest number.

## 5. Conclusion

In this article, we explored several methods to find the closest integer to a target value in a Java list, including the iterative approach, using Java Streams, and binary search for sorted lists. Each method has its strengths, depending on whether the list is sorted or whether a functional or iterative solution is preferred.

## 6. Download the Source Code

This article focused on how to find the closest integer in a Java list.