java 8 comparator

package list;

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

public class MyClass {

public static void main(String[] args) {

List people = Arrays.asList(new Person(“Remzi”, “Altan”, 23), new Person(“Ali”, “Tuzen”, 23),
new Person(“Funda”, “Tekin”, 24), new Person(“Ceyda”, “Ozbay”, 25));

/* java 1.7 verison */
//
// Collections.sort(people, new Comparator() {
//
// @Override
// public int compare(Person arg0, Person arg1) {
//
// return arg0.getName().compareTo(arg1.getName());
// }
// });

/* java 1.8 verison */

Collections.sort(people, (arg0, arg1) -> arg0.getName().compareTo(arg1.getName()));

printAll(people);

}

private static void printAll(List people) {
for (Person p : people) {

System.out.println(p.getName() + ” ” + p.getLastname() + ” ” + p.getAge());

}

}

}

interface Condition {
boolean match(Person p);
}

Advertisements

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: