In this tutorial, we will see **“Program to Multiply two matrices using Java 8 Streams?”**

### program to multiply two matrices using Java 8 Stream

Java 8 – Program to Multiply Two Matrices? (Simplest Example) Click To Tweet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.util.stream.IntStream; /** * Using Java 8, Find the Multiplication of Two Matrices * @author Deepak Verma * */ public class Program_to_Find_Multiplication_of_Two_Matrices_Java8_Example { public static void main(String[] args) { int matrix1[][]={{1,1,1},{2,2,2},{3,3,3}}; int matrix2[][]={{1,1,1},{2,2,2},{3,3,3}}; int rows1 = matrix1.length; int cols1 = matrix1[0].length; int rows2 = matrix2.length; int cols2 = matrix2[0].length; if (cols1 != rows2) { throw new IllegalArgumentException("Matrices can't be multiplied: " + cols1 + " != " + rows2); } int[][] result = new int[rows1][cols2]; IntStream.range(0, rows1).parallel().forEach(i -> IntStream.range(0, cols2).parallel().forEach(j -> result[i][j] = IntStream.range(0, cols1).parallel() .map(k -> matrix1[i][k] * matrix2[k][j]) .sum())); for (int[] row : result) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } } } |

**Output:**

1 2 3 | 6 6 6 12 12 12 18 18 18 |

### program to multiply two matrices using Java 8 Stream

Java 8 – Program to Multiply Two Matrices? (Simplest Example) Click To Tweet

**Do you like this Post? – then check my other helpful posts:**

- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8

**Other Useful References:**