Practical Comparisons
Real-World Comparison Scenarios
Practical numeric comparisons go beyond simple equality checks. This section explores advanced techniques for comparing numeric values in various programming contexts.
Comparison Strategies
graph TD
A[Practical Comparisons] --> B[Sorting]
A --> C[Range Validation]
A --> D[Conditional Logic]
A --> E[Performance Optimization]
Sorting Numeric Collections
public class NumericSortingDemo {
public static void main(String[] args) {
Integer[] numbers = {5, 2, 8, 1, 9};
// Natural sorting
Arrays.sort(numbers);
System.out.println("Sorted Ascending: " + Arrays.toString(numbers));
// Custom descending comparator
Arrays.sort(numbers, Collections.reverseOrder());
System.out.println("Sorted Descending: " + Arrays.toString(numbers));
}
}
Range Validation Techniques
| Validation Type | Description | Example |
| ------------------ | -------------------------------- | ------------------------------------- | --- | ------------ |
| Inclusive Range | Check if value is within bounds | value >= min && value <= max
|
| Exclusive Range | Check if value is outside bounds | value < min | | value > max
|
| Bounded Validation | Limit value to specific range | Math.min(Math.max(value, min), max)
|
Advanced Comparison Methods
public class RangeValidationDemo {
public static void validateAge(int age) {
// Comprehensive age validation
if (age < 0 || age > 120) {
throw new IllegalArgumentException("Invalid age: " + age);
}
// Age-based categorization
String ageGroup = determineAgeGroup(age);
System.out.println("Age Group: " + ageGroup);
}
private static String determineAgeGroup(int age) {
if (age < 18) return "Minor";
if (age >= 18 && age < 65) return "Adult";
return "Senior";
}
public static void main(String[] args) {
validateAge(25); // Adult
validateAge(70); // Senior
}
}
public class OptimizedComparisonDemo {
public static int findMaxValue(int[] numbers) {
// Efficient max value finding
return Arrays.stream(numbers)
.max()
.orElseThrow(() -> new IllegalArgumentException("Empty array"));
}
public static boolean isWithinTolerance(double value, double target, double tolerance) {
// Precise floating-point comparison
return Math.abs(value - target) <= tolerance;
}
public static void main(String[] args) {
int[] values = {10, 5, 8, 20, 3};
System.out.println("Maximum Value: " + findMaxValue(values));
System.out.println("Within Tolerance: " +
isWithinTolerance(3.14, 3.15, 0.02) // true
);
}
}
Comparison Patterns
- Use
Comparable
interface for custom object comparisons
- Leverage
Comparator
for complex sorting logic
- Implement null-safe comparison methods
- Consider performance implications of comparison strategies
Null-Safe Comparison Approach
public class NullSafeComparisonDemo {
public static <T extends Comparable<T>> int compareNullSafe(T a, T b) {
if (a == null && b == null) return 0;
if (a == null) return -1;
if (b == null) return 1;
return a.compareTo(b);
}
public static void main(String[] args) {
Integer x = 10;
Integer y = null;
System.out.println("Null-safe comparison: " + compareNullSafe(x, y));
}
}
LabEx Learning Recommendation
At LabEx, we encourage developers to master these practical comparison techniques to write more robust and efficient Java applications.