Java8 :lambda表达式初体验
package lambda; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class FirstTouchLambda { public static void main(String[] args) { String[] arr= {"Andy","Bill","Cindy","张三","李四","王五",}; for(String name:arr) { System.out.println(name); } List<String> nameList = new ArrayList<String>(Arrays.asList(arr)); // filter long n=nameList.stream().filter(name->name.startsWith("张")).count(); System.out.println("张姓人数:"+n); // loop nameList.stream().filter(name->{System.out.println(name+":"+name.length());return name.length()>0;}).count(); // Uppercase List<String> ucaseNames=nameList.stream().map(name->name.toUpperCase()).collect(Collectors.toList()); for(String name:ucaseNames) { System.out.println(name); } } }
输出:
Andy Bill Cindy 张三 李四 王五 张姓人数:1 Andy:4 Bill:4 Cindy:5 张三:2 李四:2 王五:2 ANDY BILL CINDY 张三 李四 王五
--2020-03-13--