import java.util.Collections;import java.util.List;import java.util.ArrayList;import java.util.Comparator;
编程学习网为您整理以下代码实例,主要实现:Java8示例程序,希望可以帮到各位朋友。
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
public class Java8Tester {
public static voID main(String args[]) {
List<String> names1 = new ArrayList<String>();
names1.add("Mahesh ");
names1.add("Suresh ");
names1.add("Ramesh ");
names1.add("Naresh ");
names1.add("Kalpesh ");
List<String> names2 = new ArrayList<String>();
names2.add("Mahesh ");
names2.add("Suresh ");
names2.add("Ramesh ");
names2.add("Naresh ");
names2.add("Kalpesh ");
Java8Tester tester = new Java8Tester();
System.out.println("Sort using Java 7 Syntax: ");
tester.sortUsingJava7(names1);
System.out.println(names1);
System.out.println("Sort using Java 8 Syntax: ");
tester.sortUsingJava8(names2);
System.out.println(names2);
}
//sort using java 7
private voID sortUsingJava7(List<String> names) {
Collections.sort(names, new Comparator<String>() {
@OverrIDe
public int compare(String s1, String s2) {
return s1.compareto(s2);
}
});
}
//sort using java 8
private voID sortUsingJava8(List<String> names) {
Collections.sort(names, (s1, s2) -> s1.compareto(s2));
}
}
沃梦达教程
本文标题为:Java8示例程序
基础教程推荐
猜你喜欢
- JAVA通过正则匹配html里面body标签的内容 2022-10-25
- Hello World示例 1970-01-01
- Java方法引用示例 1970-01-01
- 整数类型 1970-01-01
- Java Lambda表达式示例 1970-01-01
- Java.io.ObjectStreamField.isUnshared()方法 1970-01-01
- java.util.zip.Inflater.read()方法 1970-01-01
- Java8示例程序 1970-01-01
- Java.io.ObjectStreamField.toString()方法 1970-01-01