java 7 comparator,condition Check

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

Collections.sort(people, new Comparator() {

@Override
public int compare(Person ob1, Person ob2) {

return ob1.getName().compareTo(ob2.getName());
}

});

printCond(people, new Condition() {

@Override
public boolean match(Person p) {

return p.getLastname().startsWith(“T”);
}
});

}

private static void printCond(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

4 Comments

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

ArithmeticApp in Java 8

package com.sezo;

public class ArithmeticApp {

public static void main(String[] args) {

int result = 0;
Arithmetic add = (int a, int b) -> a + b;
result = add.calculate(3, 5);
System.out.println(result);

Arithmetic multiply = (int a, int b) -> a * b;
result = multiply.calculate(5, 4);
System.out.println(result);

Arithmetic divide = (int a, int b) -> {
if (b == 0)
return 0;
return a / b;
};
result = divide.calculate(40, 20);

System.out.println(result);

}

}

interface Arithmetic {
int calculate(int a, int b);

}

Leave a comment