Java Online Compiler
Example: FindMaxElementInColumnStream in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// FindMaxElementInColumnStream import java.util.Arrays; import java.util.NoSuchElementException; // For handling empty streams after max() public class Main { /** * Finds the maximum element in a specified column of a 2D array using Java Stream API. * Handles cases for empty matrices or invalid column indices. * * @param matrix The 2D integer array. * @param columnIndex The index of the column to search. * @return The maximum element in the specified column. * @throws IllegalArgumentException if the matrix is empty or columnIndex is invalid. * @throws NoSuchElementException if the specified column results in an empty set of numbers (e.g., all rows are shorter than columnIndex). */ public static int findMaxInColumnStream(int[][] matrix, int columnIndex) { // Step 1: Validate the matrix and column index if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { throw new IllegalArgumentException("Matrix cannot be empty or null."); } if (columnIndex < 0 || columnIndex >= matrix[0].length) { throw new IllegalArgumentException("Invalid column index. Must be between 0 and " + (matrix[0].length - 1)); } // Step 2: Create an IntStream from the matrix rows // Step 3: Filter rows that have the target column and map to the element in that column // Step 4: Find the maximum element in the resulting stream return Arrays.stream(matrix) // Stream<int[]> .filter(row -> row.length > columnIndex) // Filter out rows that are too short .mapToInt(row -> row[columnIndex]) // Map each relevant row to its element in the target column .max() // Find the maximum OptionalInt .orElseThrow(() -> new NoSuchElementException("No elements found in the specified column.")); // Handle case where column is effectively empty } public static void main(String[] args) { int[][] data = { {100, 150, 120, 180}, {200, 190, 210, 170}, { 90, 110, 130, 140}, {300, 80, 250, 220} }; // Example 1: Find max in column 1 (index 1) int targetColumn1 = 1; try { int maxInCol1 = findMaxInColumnStream(data, targetColumn1); System.out.println("Max element in column " + targetColumn1 + " (Stream): " + maxInCol1); } catch (Exception e) { System.out.println("Error (Stream) for column " + targetColumn1 + ": " + e.getMessage()); } // Example 2: Find max in column 0 (index 0) int targetColumn0 = 0; try { int maxInCol0 = findMaxInColumnStream(data, targetColumn0); System.out.println("Max element in column " + targetColumn0 + " (Stream): " + maxInCol0); } catch (Exception e) { System.out.println("Error (Stream) for column " + targetColumn0 + ": " + e.getMessage()); } // Example 3: Invalid column index int invalidColumn = 5; try { int maxInInvalidCol = findMaxInColumnStream(data, invalidColumn); System.out.println("Max element in column " + invalidColumn + " (Stream): " + maxInInvalidCol); } catch (Exception e) { System.out.println("Error (Stream) for invalid column " + invalidColumn + ": " + e.getMessage()); } // Example 4: Matrix where some rows are shorter int[][] jaggedMatrix = { {10, 20}, {50, 60, 70}, {30, 40} }; int targetColumnJagged = 2; // Column index 2 is only present in one row try { int maxInJaggedCol = findMaxInColumnStream(jaggedMatrix, targetColumnJagged); System.out.println("Max element in column " + targetColumnJagged + " (Jagged Stream): " + maxInJaggedCol); } catch (Exception e) { System.out.println("Error (Jagged Stream) for column " + targetColumnJagged + ": " + e.getMessage()); } // Example 5: Empty matrix int[][] emptyMatrix = {}; try { int maxInEmpty = findMaxInColumnStream(emptyMatrix, 0); System.out.println("Max element in empty matrix (Stream): " + maxInEmpty); } catch (Exception e) { System.out.println("Error (Stream) for empty matrix: " + e.getMessage()); } } }
Output
Clear
ADVERTISEMENTS