Get a Taste of Lambdas and Get Addicted to Streams by Venkat Subramaniam

By: Devoxx

1903   28   128154

Uploaded on 11/11/2015

The first thing visible in Java 8 is the lambdas, but the real power is in Streams. If you're interested in immersing into lambdas and streams, this is the session for you. We will start with lambda expressions, learn about how to use them and how they get implemented under the hood. Then we will swiftly move into learning about streams, the abstractions they provide, and a variety of operations we can do with them. We will conclude with a discussion about lazy evaluations, infinite streams, and parallel collections.

Comments (5):

By anonymous    2017-09-20

The helper methods from the accepted answer are not needed. Streams can be used with lambdas or usually shortened using Method References. Streams enable functional operations. map() converts the elements and collect(...) or toArray() wrap the stream back up into an array or collection.

Venkat Subramaniam's talk (video) explains it better than me.

1 Convert List<String> to List<Integer>

List<String> l1 = Arrays.asList("1", "2", "3");
List<Integer> r1 = l1.stream().map(Integer::parseInt).collect(Collectors.toList());

// the longer full lambda version:
List<Integer> r1 = l1.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());

2 Convert List<String> to int[]

int[] r2 = l1.stream().mapToInt(Integer::parseInt).toArray();

3 Convert String[] to List<Integer>

String[] a1 = {"4", "5", "6"};
List<Integer> r3 = Stream.of(a1).map(Integer::parseInt).collect(Collectors.toList());

4 Convert String[] to int[]

int[] r4 = Stream.of(a1).mapToInt(Integer::parseInt).toArray();

5 Convert String[] to List<Double>

List<Double> r5 = Stream.of(a1).map(Double::parseDouble).collect(Collectors.toList());

6 (bonus) Convert int[] to String[]

int[] a2 = {7, 8, 9};
String[] r6 = Arrays.stream(a2).mapToObj(Integer::toString).toArray(String[]::new);

Lots more variations are possible of course.

Also see Ideone version of these examples. Can click fork and then run to run in the browser.

Original Thread

By anonymous    2017-09-20

Try something like this. I found this solution in a YT video I watched yesterday. Go to about 1:58:30

https://www.youtube.com/watch?v=1OpAgZvYXLQ

List<MyObjects> myList = createObjects();


Map<String, Optional<String>> myMappedList = myList.stream()
             .collect(Collectors.groupingBy(MyObjects::getName, 
                     Collectors.mapping(MyObjects::getidSet, 
                         Collectors.maxBy((a, b) -> a.toString().length() - b.toString().length()))));

//Here's the output with your example (Using toString override in MyObjects Class):

System.out.println(myMappedList);

{CH2OH=Optional[223,256,295], CH2CH2OH=Optional[255,310]}

By "max", the comparator is looking for the idset with the longest string length.

Hope this helps, I thought this was a good question. Venkat Subramaniam thought it important enough to talk about...

Original Thread

By anonymous    2017-11-27

this is good video https://www.youtube.com/watch?v=1OpAgZvYXLQ

Original Thread

Recommended Books

    Submit Your Video

    If you have some great dev videos to share, please fill out this form.