Skip to content

Commit 32c4ad5

Browse files
committed
add lazy map example
1 parent a731424 commit 32c4ad5

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.javalearning.collection;
2+
3+
import org.apache.commons.collections4.Closure;
4+
import org.apache.commons.collections4.CollectionUtils;
5+
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
6+
import org.apache.commons.lang3.builder.ToStringStyle;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Created by renqun.yuan on 2015/11/4.
13+
*/
14+
public class CollectionUtilExample {
15+
public static void main(String[] args) {
16+
new CollectionUtilExample().test();
17+
}
18+
19+
public void test() {
20+
List<A> aList = new ArrayList<A>();
21+
A a = new A(1);
22+
aList.add(a);
23+
aList.add(new A(2));
24+
CollectionUtils.forAllDo(aList, new Closure<A>() {
25+
@Override
26+
public void execute(A input) {
27+
input.setId(5);
28+
}
29+
});
30+
31+
System.out.println(aList);
32+
}
33+
34+
class A {
35+
private Integer id;
36+
37+
public Integer getId() {
38+
return id;
39+
}
40+
41+
public void setId(Integer id) {
42+
this.id = id;
43+
}
44+
45+
public A(Integer id) {
46+
47+
this.id = id;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return ReflectionToStringBuilder.toString(this,ToStringStyle.JSON_STYLE);
53+
}
54+
}
55+
56+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.javalearning.collection;
2+
3+
import org.apache.commons.collections4.Transformer;
4+
import org.apache.commons.collections4.map.LazyMap;
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* Created by renqun.yuan on 2015/11/4.
12+
*/
13+
public class LazyMapExample {
14+
public static void main(String[] args) {
15+
Map<String, Integer> map = new HashMap<>();
16+
LazyMap<String, Integer> lazyMap = LazyMap.lazyMap(map, new Transformer<String, Integer>() {
17+
@Override
18+
public Integer transform(String input) {
19+
if (StringUtils.isNotBlank(input)) {
20+
return Integer.parseInt(input);
21+
}
22+
return null;
23+
}
24+
});
25+
26+
lazyMap.get("123");
27+
28+
lazyMap.get("123");
29+
System.out.println(lazyMap.size());
30+
lazyMap.get("12");
31+
32+
System.out.println(lazyMap.size());
33+
}
34+
}

0 commit comments

Comments
 (0)