|
| 1 | +package com.guava.lambda; |
| 2 | + |
| 3 | +import com.entity.Person; |
| 4 | +import com.google.common.base.Function; |
| 5 | +import com.google.common.base.Predicate; |
| 6 | +import com.google.common.base.Predicates; |
| 7 | +import com.google.common.collect.Collections2; |
| 8 | +import com.google.common.collect.Iterables; |
| 9 | +import com.google.common.collect.Lists; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static com.google.common.base.Predicates.and; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * ClassName: GuavaLambda |
| 19 | + * Description: |
| 20 | + * Date: 2016/5/28 9:17 |
| 21 | + * |
| 22 | + * @author SAM SHO |
| 23 | + * @version V1.0 |
| 24 | + */ |
| 25 | +public class GuavaLambda { |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + GuavaLambda lambda = new GuavaLambda(); |
| 29 | +// lambda.test(); |
| 30 | +// lambda.test1(); |
| 31 | +// lambda.test2(); |
| 32 | + lambda.test3(); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + public void test() { |
| 37 | + List<Person> people = new ArrayList<Person>(); |
| 38 | + people.add(new Person("bowen",27)); |
| 39 | + people.add(new Person("bob", 20)); |
| 40 | + people.add(new Person("Katy", 18)); |
| 41 | + people.add(new Person("Logon", 24)); |
| 42 | + |
| 43 | + System.out.println(people); |
| 44 | + |
| 45 | + /** 选取年龄 > 20 **/ |
| 46 | + List<Person> oldPeople = new ArrayList<Person>(); |
| 47 | + for (Person person : people) { |
| 48 | + if (person.getAge() >= 20) { |
| 49 | + oldPeople.add(person); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + System.out.println(oldPeople); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * newArrayList()方法 |
| 58 | + */ |
| 59 | + public void test1() { |
| 60 | + List<Person> people = Lists.newArrayList(new Person("bowen", 27), |
| 61 | + new Person("bob", 20), |
| 62 | + new Person("Katy", 18), |
| 63 | + new Person("Logon", 24)); |
| 64 | + |
| 65 | + System.out.println(people); |
| 66 | + |
| 67 | + /** |
| 68 | + * 这就是典型的filter模式。filter即从一个集合中根据一个条件筛选元素。 |
| 69 | + * 其中person.getAge() >=20就是这个条件。Guava为这种模式提供了一个filter的方法。所以我们可以这样写。 |
| 70 | + * |
| 71 | + * Predicate 里面只有一个apply方法,接收一个泛型的实参,返回一个boolean值。 |
| 72 | + * 由于java世界中函数并不是一等公民,所以我们无法直接传递一个条件函数,只能通过Predicate这个类包装一下。 |
| 73 | + * |
| 74 | + * Iterables.filter 与 Collections2.filter 一样可以用 |
| 75 | + * |
| 76 | + **/ |
| 77 | + List<Person> oldPeople = Lists.newArrayList(Iterables.filter(people, new Predicate<Person>() { |
| 78 | + public boolean apply(Person person) { |
| 79 | + return person.getAge() >= 20; |
| 80 | + } |
| 81 | + })); |
| 82 | + |
| 83 | + System.out.println(oldPeople); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * And Predicate |
| 88 | + */ |
| 89 | + public void test2() { |
| 90 | + List<Person> people = Lists.newArrayList(new Person("bowen", 27), |
| 91 | + new Person("bob", 20), |
| 92 | + new Person("Katy", 18), |
| 93 | + new Person("Logon", 24)); |
| 94 | + System.out.println(people); |
| 95 | + |
| 96 | + List<Person> filteredPeople = Lists.newArrayList(Collections2.filter(people, Predicates.and(ageBiggerThan(20), nameContains("b")))); |
| 97 | + |
| 98 | + System.out.println(filteredPeople); |
| 99 | + } |
| 100 | + |
| 101 | + private Predicate<Person> ageBiggerThan(final int age) { |
| 102 | + return new Predicate<Person>() { |
| 103 | + public boolean apply(Person person) { |
| 104 | + return person.getAge() >= age; |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + private Predicate<Person> nameContains(final String str) { |
| 110 | + return new Predicate<Person>() { |
| 111 | + public boolean apply(Person person) { |
| 112 | + return person.getName().contains(str); |
| 113 | + } |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Map(transform):map pattern |
| 119 | + * 就是将数组中的所有元素映射为另一种元素的列表 |
| 120 | + **/ |
| 121 | + public void test3() { |
| 122 | + |
| 123 | + List<Person> people = Lists.newArrayList(new Person("bowen", 27), |
| 124 | + new Person("bob", 20), |
| 125 | + new Person("Katy", 18), |
| 126 | + new Person("Logon", 24)); |
| 127 | + |
| 128 | + /*求People列表中的所有人名。程序员十有八九都会这样写。*/ |
| 129 | + List<String> names = new ArrayList<String>(); |
| 130 | + for (Person person : people) { |
| 131 | + names.add(person.getName()); |
| 132 | + } |
| 133 | + System.out.println(names); |
| 134 | + |
| 135 | + /** |
| 136 | + * Function是另外一种用于封装函数的接口对象。 |
| 137 | + * 它与Predicate非常相似,但不同的是它接收两个泛型, |
| 138 | + * apply方法接收一种泛型实参,返回值是另一种泛型值。正是这个apply方法定义了数组间元素一对一的map规则。 |
| 139 | + **/ |
| 140 | + List<String> names2 = Lists.newArrayList(Collections2.transform(people, new Function<Person, String>() { |
| 141 | + public String apply(Person person) { |
| 142 | + return person.getName(); |
| 143 | + } |
| 144 | + })); |
| 145 | + System.out.println(names2); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | +} |
0 commit comments