Skip to content

Commit ff58595

Browse files
committed
【Guava】lambda
1 parent 2938fab commit ff58595

6 files changed

Lines changed: 492 additions & 136 deletions

File tree

Guava/.idea/workspace.xml

Lines changed: 291 additions & 133 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Guava/Guava.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
</content>
1010
<orderEntry type="inheritedJdk" />
1111
<orderEntry type="sourceFolder" forTests="false" />
12+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
13+
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
1214
<orderEntry type="library" name="Maven: com.google.guava:guava:18.0" level="project" />
1315
</component>
1416
</module>

Guava/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<guava.version>18.0</guava.version>
14-
<log4j.version>2.5</log4j.version>
15-
<fileupload.version>1.2.2</fileupload.version>
14+
<junit.version>4.11</junit.version>
1615
</properties>
1716

1817
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>${junit.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
1925
<dependency>
2026
<groupId>com.google.guava</groupId>
2127
<artifactId>guava</artifactId>
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.entity;
2+
3+
public class Person {
4+
public String getName() {
5+
return name;
6+
}
7+
8+
public void setName(String name) {
9+
this.name = name;
10+
}
11+
12+
public int getAge() {
13+
return age;
14+
}
15+
16+
public void setAge(int age) {
17+
this.age = age;
18+
}
19+
20+
private String name;
21+
private int age;
22+
23+
public Person(String name, int age) {
24+
this.name = name;
25+
this.age = age;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Person{" +
31+
"name='" + name + '\'' +
32+
", age=" + age +
33+
'}';
34+
}
35+
}

Guava/src/main/java/com/entity/Presider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ public class Presider implements Serializable {
2626

2727
public Presider() {}
2828

29-
public int getId() {
29+
public Presider(String name, String jobNumber) {
30+
this.name = name;
31+
this.jobNumber = jobNumber;
32+
}
33+
34+
public int getId() {
3035
return id;
3136
}
3237

0 commit comments

Comments
 (0)