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 Introducing Lambdas

Error

I'm not sure why I keep getting an error in usingLamdasInLongForm, I tried carefully checking what I'm missing but at this point I just can't find the flaw. please help. Thanks!

import java.util.Collections; import java.util.Comparator; import java.util.List;

public class Main {

public static void usingAnonymouslineClass () {
    List<Book> books = Books.all();
    Collections.sort(books, new Comparator<Book>() {
        @Override
        public int compare(Book b1, Book b2) {
            return b1.getTitle().compareTo(b2.getTitle());
        }
    });

    for (Book book : books) {
        System.out.println(book);
    }
}

public static void usingLamdasInLongForm() {
    List<Book> books = Books.all();
    Collections.sort(books, (Book b1, Book b2) -> {
        return b1.getTitle().compareTo(b2.getTitle());

    });

    for (Book book : books) {
        System.out.println(book);
    }
}


public static void main(String[] args) {
// write your code here
    usingLambdasInLongForm();

}

}

Seth Kroger
Seth Kroger
56,414 Points

Telling us what the error you're getting and at what line would be a big help in figuring out the issue.

Hi Seth,

the error I'm getting "Overrides method in java.util.Comparator" at line 25.

public static void usingLambdasInLongForm() { List<Book> books = Books.all(); Collections.sort(books, (Book b1, Book b2) -> { return b1.getTitle().compareTo(b2.getTitle()); });

Thanks