1pietras
1pietras

Reputation: 23

Comparator in java 8

Recently I've updated Java to version 8, but I have problem with Comparator (in 7 version everything worked perfectly):

    lastplace = Collections.max(place,
                    new Comparator<Place>() {

                public int compare(Place o1,
                        Place o2) {
                    return (o1.getDateFrom()
                            .compareTo(o2.getDateFrom()));
                }
            });

Now Comparator needs to inherit other methods (thenComparingInt, thenComparingLong etc.), but I don't need them

How to solve this in few lines of code ?

Upvotes: 1

Views: 295

Answers (2)

Sachin Janani
Sachin Janani

Reputation: 1319

The new mehods in Comparator class are default that you dont need to implement because it provides default implementation.I think the problem is with your IDE due to compiler compliance level set to 1.7.Just change the compiler level to 1.8 and if it not there upgrade your IDE.

Upvotes: 2

Kasper Ziemianek
Kasper Ziemianek

Reputation: 1349

According to javadocs http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html those methods are marked as default, so You dont have to provide implementation. More info about default interface methods :http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/.

You can use lamda expression to simply implement Comparator interface. This might help : http://www.dreamsyssoft.com/java-8-lambda-tutorial/comparator-tutorial.php

Upvotes: 1

Related Questions