标签:tst rgs sort etc ati read print 字符 返回值
@FunctionalInterface public interface FunctionInterface { void show(); }
public class Demo { public static void main(String[] args) { startThread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "启动一个线程"); } }); startThread(() -> System.out.println(Thread.currentThread().getName() + "启动一个线程")); } public static void startThread(Runnable runnable) { Thread thread = new Thread(runnable); thread.start(); } }
public static Comparator<String> getComparator() { // return new Comparator<String>() { // @Override // public int compare(String s1, String s2) { // return s1.length()-s2.length(); // } // }; return (s1,s2)->s1.length()-s2.length(); } // 将下面集合中的字符串按照长度排序 ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("aaaaa"); list.add("aaaaaa"); list.add("aaaa"); System.out.println("排序之前:"+list);//[aaa, aaaaa, aaaaaa, aaaa] Collections.sort(list,getComparator()); System.out.println("排序之后:"+list);//[aaa, aaaa, aaaaa, aaaaaa]
public class Demo2 { public static void main(String[] args) { String str = getStr(new Supplier<String>() { @Override public String get() { return "字符串"; } }); System.out.println(str); String str1 = getStr(() -> "字符串"); System.out.println(str1); } public static String getStr(Supplier<String> supplier) { return supplier.get(); } }
public static Integer getMax(Supplier<Integer> supplier) { return supplier.get(); } int []num={15,55,88,33,66}; Integer max = getMax(new Supplier<Integer>() { @Override public Integer get() { int max = num[0]; for (int i = 0; i < num.length; i++) { if (num[i] > max) { max = num[i]; } } return max; } }); Integer max1 = getMax(() -> { int maxValue = num[0]; for (int i = 0; i < num.length; i++) { if (num[i] > maxValue) { maxValue = num[i]; } } return maxValue; }); System.out.println(max); System.out.println(max1);
标签:tst rgs sort etc ati read print 字符 返回值
原文地址:https://www.cnblogs.com/luzhanshi/p/13200429.html