Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring Basics Modeling, Storing, and Presenting Data Create a Data Repository Component

Test Test
Test Test
21,581 Points

Java 8 alternative for findByName method (lambdas and streams)

Since Intellij Idea can run with Java 8 (lambdas and aggregate operations), does it make sense to use them as a better choice than for-each loop? It works the same, but it does not iterate all items in the List -> saves time and battery.

Here is the method for Java8:

public Gif findByNameJava8 (String name) { 
     return ALL_GIFS 
    .filter(e -> e.getName().equals(name)) 

Prerequisites: 1) set sourceCompatibility = 1.8 in your build.gradle file; 2) set language level to 8 and SDK to 1.8 (Ctrl+Shift+Alt+S on Windows, tabs Project and Module (Source)) 3) You Java version (JDK) must be not lower than 1.8.

PS: official Java documentation about for-each loop and aggregate operations: https://docs.oracle.com/javase/tutorial/collections/streams/index.html

Brandon Adams
Brandon Adams
10,325 Points

I did all the prereqs but ItelliJ still thinks I'm using 1.5, can't do lambdas and I'm having errors with LocalDate too. Any ideas what could be preventing 1.8 from being used?

Jeremiah Shore
Jeremiah Shore
31,168 Points

Brandon Adams check your build.gradle. It can default sourceCompatibility to 1.5 when we are using 1.8. Make sure you do a Gradle project refresh after.

Boban Talevski
Boban Talevski
24,793 Points

Just a virtual +1 for this (since I can't upvote your post)!

I did the same thing as well (my implementation is exactly the same as yours) and nice to see that someone else thought of it, by which I could also confirm I was on the right track thinking this was a good spot for testing my Java 8 skills :).

I think it should've been done this way in the course, just so we can get additional practice on Java 8 features and get a "hey, this is a nice spot to use a Java 8 feature" moment instead of doing it the old school way.

3 Answers

Hi Konstankin,

This is really nice. When he implemented the enhancement for loop first thing I thought was, I can use a lambda here. You mentioned something interesting, so with the stream().filter().findFirst() it does not go through the whole list?

How does that happen? I mean, like if the element is the last one, what would be the behaviour?


Jeremiah Shore
Jeremiah Shore
31,168 Points

Check out the JavaDoc link he shared, it's well worth reading.

In particular, see the section called "Differences Between Aggregate Operations and Iterators". Apparently, aggregate operations can use Parallelism because streams use internal iteration, as opposed to to the means defined by the Iterator interface which collections use. This is all new to me, except lambdas in general, and now the benefit of streams is a lot clearer!

Thanks @konstantinkochetov for sharing!

Ilja Torikka
Ilja Torikka
2,178 Points

It's the first one, which intermediate operation filter(e -> e.getName().equals(name)) finds.