函数式接口作为方法的返回值
package LambdaTest.LambdaTest04; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; /** * FileName: CompartorDemo * Author: lps * Date: 2022/4/5 16:19 * Sign:刘品水 Q:1944900433 */ public class CompartorDemo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<>(); array.add("aaa"); array.add("a"); array.add("bb"); array.add("c"); System.out.println("排序前"+array); Collections.sort(array); System.out.println("排序后"+array); Collections.sort(array,getComparator()); System.out.println("排序后"+array); //getComparator(()->); } private static Comparator<String> getComparator(){ //匿名内部类 // Comparator<String> comp=new Comparator<String>() { // @Override // public int compare(String s1, String s2) { // return s1.length()-s2.length(); // } // }; // return comp; // return new Comparator<String>() { // @Override // public int compare(String s1, String s2) { // return s1.length()-s2.length(); // } // }; //Lambda表达式 // return (String s1,String s2)->{ // return s1.length()-s2.length(); // }; return (s1,s2)->s1.length()-s2.length(); //return Comparator.comparingInt(String::length); } }