How to implement a reverse mapping Java using lambdas? -
As far as I know, in Java 8, the mapping works as follows
For example:
List & lt; String & gt; ObjList = Arrays.asList ("1," 2, 3, 4, 5, 6, 7); ObjList.stream (). Map (i - & gt; i) For every (i -> System.out.print ("+ i)"); Output:
1 2 3 4 5 6 7 But my question is: how Can I reverse mapping? Like
Then it will be output
7 6 5 4 3 2 1 Or is it not possible to do this?
In this example, I submit the list to one.
Enters Linkedist (Double End Quote) which allows me to attach items to the last-in-first-out (LIFO) order.
I store the collected method using Linkedist List :: Adjust that reads the item, close the section at the top of the list.
public static zero main (last string ... args) {final list & lt; String & gt; ObjList = Arrays.asList ("1," 2, 3, 4, 5, 6, 7); Last list & lt; String & gt; Reverse = objList.stream () .collect (linked list: new, linked list: addFirst, linkedlist: addAll); // [7, 6, 5, 4, 3, 2, 1] System.out.println (reverse); } The solution below is OK, but I do not like to close the original list.
public static zero main (final string .. rgs) {final list & lt; String & gt; ObjList = Arrays.asList ("1," 2, 3, 4, 5, 6, 7); Last list & lt; String & gt; Reversed = IntStream.range (- (objList.size) - 1), 1) .mapToObj (i -> objList.get (-i)) .collect (collectors list); // [7, 6, 5, 4, 3, 2, 1] System.out.println (reverse); }
Comments
Post a Comment