How can I achieve the following array print using Java 8's lambda expressions?
int[] values = new int[16];
// Populate values
for (int value : values) {
System.out.println(Integer.toUnsignedString(value, 16));
}
An array is a fixed size element of the same type. The lambda expressions can also be used to initialize arrays in Java.
// Create a list from a String array List list = new ArrayList(); String[] strArr = { "Chris", "Raju", "Jacky" }; for( int i = 0; i < strArr. length; i++ ) { list. add( strArr[i]); } // // Print the key and value of Map using Lambda expression // list. forEach((value) -> {System.
Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.
We cannot print array elements directly in Java, you need to use Arrays. toString() or Arrays. deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.
Arrays.stream(values)
.mapToObj(i -> Integer.toUnsignedString(i, 16))
.forEach(System.out::println);
String[] nums = {"three","two","one"};
Arrays.stream(nums).forEach(num -> System.out.println(num));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With