Noor :
I have a list of array of 2 objects:
List<Object[2]>
Where object[0] is an Integer and object[1] is a String.
How can I stream the list and apply different functions on each object? So that, the result will be an array having:
result[0] = multiplication of all object[0]
result[1] = concatenation of all object[1]
Vincent Passau :
You can achieve this with reduce()
:
public void testStacko() {
List<Object[]> list = new ArrayList<>();
list.add(new Object[] {1, "foo"});
list.add(new Object[] {6, "|bar"});
list.add(new Object[] {15, "|baz"});
Object[] array = list.stream()
.reduce(
(obj1, obj2) ->
new Object[] {(int) obj1[0] * (int) obj2[0],
(String) obj1[1] + (String) obj2[1]
}
)
.get();
System.out.println(array[0]); // 90
System.out.println(array[1]); // foo|bar|baz
}