Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,403 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