Lambda部分操作

it2023-05-19  71

Lambda表达式 包括Lambda内置接口和Steam等操作

// A code block import java.util.*; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collector; import java.util.stream.Collectors; public class E { public static void main(String[] args) { List<Person>list=new ArrayList(){ { add(new Person("Mike",'m',5000,21)); add(new Person("Rose",'f',15000,24)); add(new Person("Mary",'f',2000,18)); add(new Person("Jhon",'m',20000,27)); add(new Person("Alice",'f',30000,30)); } }; //Consumer:accept(T t) 核心方法 foreach Consumer<Person>con=(p)->System.out.println(p.getName()); con.accept(new Person("Alice",'f',3000,30)); list.forEach(p->System.out.println(p.getName())); System.out.println("-------------------------------------"); list.forEach(p-> {p.setSalary(p.getSalary()+p.getSalary()*5/100); //System.out.println(p.getName()+" "+p.getSalary()); System.out.printf("%s----%d\n",p.getName(),p.getSalary()); } ); //Predicate:test(T t) filter // Predicate<String> pre=s->s.length()>3; System.out.println(pre.test("FOO")); System.out.println(pre.negate().test("FOO"));//negate非,否定 //判断 Predicate<String>pre2= Objects::isNull; System.out.println(pre2.test(null)); Predicate<String>pre1= String::isEmpty; System.out.println(pre1.test("")); //Comporator:compare(t1,t2): max min sorted //排序 list.stream().sorted((p1,p2)->p1.getName().compareTo(p2.getName())) .collect(Collectors.toList()) .forEach(s->System.out.println(s.getName())); System.out.println(list.stream().max((p1,p2)->p1.getSalary()-p2.getSalary()).get().getName());//工资最高 System.out.println(list.stream().min((p1,p2)->p1.getSalary()-p2.getSalary()).get().getName());//工资最低 System.out.println(list.stream().map(Person::getName).collect(Collectors.joining(":"))); //Function apply(T) map // 三种不同的集合,HashSet HashMap TreeSet list.stream().map(Person::getName).collect(Collectors.toSet()).forEach(System.out::println); list.stream().collect(Collectors.toMap(Person::getName,Person::getSalary)).forEach((k,v)->System.out.println(k+v)); list.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new)).forEach(System.out::println); //Suppliers Supplier<Person>supplier=Person::new;//支持Person的::new动作 Person person=supplier.get();// person.setName("Pike"); System.out.println(person.getName()); //map reduce示例 List<Integer> costBeforeTax=Arrays.asList(100,200,300,400,500); costBeforeTax.stream().map(x->x+x*0.12).forEach(System.out::println); System.out.println(costBeforeTax.stream().map(x->x+x*0.12).reduce((s,p)->s+p).get());//所有的值合成一个 //match示例 匹配 System.out.println(list.stream().anyMatch(p->p.getName().startsWith("Mike")));//有一个叫Mike System.out.println(list.stream().allMatch(p->p.getName().startsWith("Mike")));//全叫Mike System.out.println(list.stream().noneMatch(p->p.getName().startsWith("Mike")));//全都不叫Mike //Count 总结其个数 System.out.println(list.stream().filter(p->p.getName().startsWith("A")).count());//前缀为A的有几个 //parallelStream 并行流 list.parallelStream().forEach(s->System.out.println(s.getName())); list.parallelStream().forEachOrdered(s->System.out.println(s.getName())); // Stream-ToIntFunction List<Integer> numbers=Arrays.asList(1,2,3,4,5,6,7,8,9,10); IntSummaryStatistics s=numbers.parallelStream().mapToInt(x->x).summaryStatistics(); System.out.println(s.getMax()); System.out.println(s.getAverage()); System.out.println(s.getMin()); System.out.println(s.getCount()); System.out.println(s.getSum()); } }
最新回复(0)