参考:http://www.cnblogs.com/andywithu/p/7344507.html
class Student{
private String name;
private Double score;
public Student(String name, Double score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public Double getScore() {
return score;
}
public void setName(String name) {
this.name = name;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "{"
+ "\"name\":\"" + name + "\""
+ ", \"score\":\"" + score + "\""
+ "}";
}
}
@Test
public void test1(){
List<Student> studentList = new ArrayList<Student>(){
{
add(new Student("stu1",100.0));
add(new Student("stu2",97.0));
add(new Student("stu3",96.0));
add(new Student("stu4",95.0));
}
};
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Double.compare(o1.getScore(),o2.getScore());
}
});
System.out.println(studentList);
}
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
}
public void test1_(){
List<Student> studentList = new ArrayList<Student>(){
{
add(new Student("stu1",100.0));
add(new Student("stu2",97.0));
add(new Student("stu3",96.0));
add(new Student("stu4",95.0));
}
};
Collections.sort(studentList,(s1,s2)-> Double.compare(s1.getScore(),s2.getScore()));
System.out.println(studentList);
}
public void testThread(){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello, i am thread!");
}
}).start();
}
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object‘s
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
public void testThread_(){
new Thread(()-> System.out.println("hello, i am thread!")).start();
}
@FunctionalInterface
public interface MyFunctionalInterface {
public void single(String msg);
}
/**
* 需要单个参数
*/
public static void testOnePar(MyFunctionalInterface myFunctionalInterface){
myFunctionalInterface.single("msg");
}
/**
* 一个参数,可以省略参数的括号
*/
@Test
public void testOneParameter(){
testOnePar(x-> System.out.println(x));
}
/**
* 需要单个参数
*/
public static void testOnePar1(Consumer unaryOperator){
unaryOperator.accept("msg");
}
原文:http://www.cnblogs.com/ycmxm/p/7345208.html