lambda
Posted paytons
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lambda相关的知识,希望对你有一定的参考价值。
1 public class TestPerson { 2 List<Person> javaProgrammers = new ArrayList<Person>() { 3 { 4 add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 43, 2300)); 5 add(new Person("Tamsen", "Brittany", "Java programmer", "female", 23, 1500)); 6 add(new Person("Floyd", "Donny", "Java programmer", "male", 33, 1800)); 7 add(new Person("Sindy", "Jonie", "Java programmer", "female", 32, 1600)); 8 add(new Person("Vere", "Hervey", "Java programmer", "male", 22, 1200)); 9 add(new Person("Maude", "Jaimie", "Java programmer", "female", 27, 1900)); 10 add(new Person("Shawn", "Randall", "Java programmer", "male", 30, 2300)); 11 add(new Person("Jayden", "Corrina", "Java programmer", "female", 35, 1700)); 12 add(new Person("Palmer", "Dene", "Java programmer", "male", 33, 2300)); 13 add(new Person("Addison", "Pam", "Java programmer", "female", 34, 10)); 14 } 15 }; 16 17 //遍历 18 @Test 19 public void ergodic() { 20 javaProgrammers.forEach(person -> System.out.println(person.getFirstName())); 21 } 22 23 //给所有的程序员加薪5% 24 @Test 25 public void addSalary() { 26 javaProgrammers.forEach(person -> person.setSalary(person.getSalary() + person.getSalary() * 5 / 100)); 27 javaProgrammers.forEach(person -> System.out.println(person.getFirstName() + "工资是:" + person.getSalary())); 28 } 29 30 @Test 31 public void addSalary2() { 32 Consumer<Person> consumer = person -> { 33 person.setSalary(person.getSalary() + person.getSalary() * 5 / 100); 34 }; 35 javaProgrammers.forEach(consumer); 36 javaProgrammers.forEach(person -> System.out.println(person.getFirstName() + "工资是:" + person.getSalary())); 37 } 38 39 //过滤----工资大于2000的员工 40 @Test 41 public void filterDemo() { 42 javaProgrammers.stream().filter(person -> person.getSalary() >= 2000). 43 forEach(person -> System.out.println(person.getFirstName() + "工资是:" + person.getSalary())); 44 } 45 46 //自定义过滤器 47 @Test 48 public void selfDefineFilter() { 49 Predicate<Person> predicate = person -> person.getSalary() >= 2000; 50 Predicate<Person> predicate1 = person -> person.getAge() >= 40; 51 javaProgrammers.stream().filter(predicate).filter(predicate1). 52 forEach(person -> System.out.println(person.getFirstName() + "年龄是" + person.getAge() + "工资是:" 53 + person.getSalary())); 54 } 55 56 //限制打印的个数 57 @Test 58 public void limitDemo() { 59 javaProgrammers.stream().filter(person -> person.getGender().equals("female")).limit(3) 60 .forEach(person -> System.out.println(person.getFirstName() + "性别是" + person.getGender())); 61 } 62 63 //工资最高的前3位,如果工资相同按年龄排序,并放在一个集合中 64 @Test 65 public void limit3Demo() { 66 List<Person> collect = javaProgrammers.stream().sorted((p1, p2) -> ( 67 p2.getSalary() - p1.getSalary() == 0 ? p2.getAge() - p1.getAge() : p2.getSalary() - p1.getSalary() 68 )).limit(3).collect(Collectors.toList()); 69 System.out.println(Arrays.deepToString(collect.toArray())); 70 } 71 72 //打印工资最高和最小的 73 @Test 74 public void minAndMaxSarlay() { 75 Person person = javaProgrammers.stream().min((p1, p2) -> (p1.getSalary() - p2.getSalary())).get(); 76 System.out.println(person); 77 Person person1 = javaProgrammers.stream().max((p1, p2) -> (p1.getSalary() - p2.getSalary())).get(); 78 System.out.println(person1); 79 }
以上是关于lambda的主要内容,如果未能解决你的问题,请参考以下文章
使用 savedInstanceState 保存和恢复 Kotlin lambda
Android开发太难了:Java Lambda ≠ Android Lambda (上)
Android开发太难了:Java Lambda ≠ Android Lambda (上)
Android开发太难了:Java Lambda ≠ Android Lambda (上)