需求
查询用户最近的中奖信息 并返回中奖名单列表
纯Java7实现
// 返回用户最近4条的中奖纪录
List<UserWinning> uwList = Lists.newArrayList(new UserWinning(userId, 1), new UserWinning(userId, 2), new UserWinning(userId, 3), new UserWinning(userId, 1));
//提取奖品ID
List<Integer> awardIdLsit = new ArrayList<>(uwList.size());
for(UserWinning uw : uwList){
awardIdLsit.add(uw.getAwardId());
}
//根据奖品ID查询奖品
List<Award> awardList = Lists.newArrayList(new Award(1, "奖品一"), new Award(2, "奖品二"), new Award(3, "奖品三"));
//List --> Map 得到奖品ID和奖品名称Map
Map<Integer, String> awardIdNameMap = new HashMap<>(awardList.size());
for(Award a : awardList){
awardIdNameMap.put(a.getId(), a.getName());
}
// 返回奖品名称列表
List<String> nameList = new ArrayList<>(awardIdLsit.size());
for(Integer id : awardIdLsit){
nameList.add(awardIdNameMap.get(id));
}
System.out.println(nameList);
评价
感觉非常繁琐 特别是当你习惯了Java8的函数式编程后
Guava实现
List<UserWinning> uwList = Lists.newArrayList(new UserWinning(userId, 1), new UserWinning(userId, 2), new UserWinning(userId, 3), new UserWinning(userId, 1));
//提取奖品ID
ImmutableList<Integer> awardIdList = from(uwList).transform(new Function<UserWinning, Integer>() {
@Override
public Integer apply(UserWinning input) {
return input.getAwardId();
}
}).toList();
System.out.println(awardIdList);
//根据奖品ID查询奖品
List<Award> awardList = Lists.newArrayList(new Award(1, "奖品一"), new Award(2, "奖品二"), new Award(3, "奖品三"));
//List --> Map 得到奖品ID和奖品Map
ImmutableMap<Integer, Award> idAwardMap = uniqueIndex(awardList, new Function<Award, Integer>() {
@Override
public Integer apply(Award input) {
return input.getId();
}
});
// 返回奖品名称列表
ImmutableList<String> awardNameList = from(awardIdList).transform(new Function<Integer, String>() {
@Override
public String apply(Integer input) {
return idAwardMap.get(input).getName();
}
}).toList();
System.out.println(awardNameList);
评价
代码不够简洁, 看的时候还是容易被这些匿名内部类干扰,虽然写的时候也很流畅。优点是返回的集合都是不可变的。
Lambdaj实现
List<UserWinning> uwList = Lists.newArrayList(new UserWinning(userId, 1), new UserWinning(userId, 2), new UserWinning(userId, 3), new UserWinning(userId, 1));
//提取awardIdList
List<Integer> awardIdList = extract(uwList, on(UserWinning.class).getAwardId());
System.out.println(awardIdList);
List<Award> awardList = Lists.newArrayList(new Award(1, "奖品一"), new Award(2, "奖品二"), new Award(3, "奖品三"));
//List -> Map
Map<Integer, Award> idAwardMap = index(awardList, on(Award.class).getId());
//得到奖品名称列表
List<String> nameList = convert(awardIdList, new Converter<Integer, String>() {
@Override
public String convert(Integer from) {
return idAwardMap.get(from).getName();
}
});
System.out.println(nameList);
评价
看起来很简洁, 一目了然。但对泛型支持的不如Guava, 如List转Map代码自动生成的是Map 需要显式修改为Map。 另外也不确定相比Guava性能如何。
参考文档
https://code.google.com/archive/p/lambdaj/wikis/LambdajFeatures.wiki
因为线上使用的是
Java7
, 不能使用Java8的语法,故想选择一些工具类简化Java7的开发。 不确定该选用
Gauva
还是
LambdaJ
?