从0到1使用Guava(Collection系列)

一、Lists.transform转换ListA为ListB(非guava)

某个List中放有Info,其定义如:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.dianping.overseas.poseidon.audit.remote;

/**
*
* Created by qiuyongchen on 2017/1/24.
*/
public class Info {

public String Id;

public int type;

}

存放该Info的List定义如下:

1
List<Info> infoList = Lists.newArrayList();

要从infoList提取出type,组成另一个List,不需要用for循环,只需使用apache的CollectionUtils工具包,比如:

1
2
3
4
5
6
7
List<Integer> typeList = Lists.transform(infoList, new Function<Info, Integer>() {
@Nullable
@Override
public Integer apply(@Nullable Info info) {
return info.type;
}
});