Archive for category JAVA 8

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()));

/* java 1.7 verison */

// printCondition(people, new Condition() {
//
// @Override
// public boolean match(Person p) {
//
// return p.getLastname().startsWith(“T”);
// }
// });

/* java 1.8 verison */

printCondition(people, p1-> p1.getLastname().startsWith(“T”));

}

private static void printCondition(List people, Condition condition) {
for (Person p : people) {
if (condition.match(p)) {
System.out.println(p.getName() + ” ” + p.getLastname() + ” ” + p.getAge());
}

}

}

}

interface Condition {
boolean match(Person p);
}

Advertisements

Leave a comment

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);
}

Leave a comment

Thread java 8

package com.sezo.thread;

public class ThreadExmaple {

public static void main(String[] args) {

Thread thread = new Thread(new Runnable() {

@Override
public void run() {

System.out.println(“Traditional way run thread!”);

}
});

thread.run();

Thread threadLamda = new Thread(() -> System.out.println(“Thread run with lambda”));
threadLamda.run();
}

}

Leave a comment