查询用户最近的中奖信息 并返回中奖名单列表
// 返回用户最近4条的中奖纪录 List uwList = Lists.newArrayList(new UserWinning(userId, 1), new UserWinning(userId, 2), new UserWinning(userId, 3), new UserWinning(userId, 1)); //提取奖品ID List awardIdLsit = new ArrayList<>(uwList.size()); for(UserWinning uw : uwList){ awardIdLsit.add(uw.getAwardId()); } //根据奖品ID查询奖品 List awardList = Lists.newArrayList(new Award(1, "奖品一"), new Award(2, "奖品二"), new Award(3, "奖品三")); //List --> Map 得到奖品ID和奖品名称Map Map awardIdNameMap = new HashMap<>(awardList.size()); for(Award a : awardList){ awardIdNameMap.put(a.getId(), a.getName()); } // 返回奖品名称列表 List nameList = new ArrayList<>(awardIdLsit.size()); for(Integer id : awardIdLsit){ nameList.add(awardIdNameMap.get(id)); } System.out.println(nameList);
感觉非常繁琐 特别是当你习惯了Java8的函数式编程后
List uwList = Lists.newArrayList(new UserWinning(userId, 1), new UserWinning(userId, 2), new UserWinning(userId, 3), new UserWinning(userId, 1)); //提取奖品ID ImmutableList awardIdList = from(uwList).transform(new Function() { @Override public Integer apply(UserWinning input) { return input.getAwardId(); } }).toList(); System.out.println(awardIdList); //根据奖品ID查询奖品 List awardList = Lists.newArrayList(new Award(1, "奖品一"), new Award(2, "奖品二"), new Award(3, "奖品三")); //List --> Map 得到奖品ID和奖品Map ImmutableMap idAwardMap = uniqueIndex(awardList, new Function() { @Override public Integer apply(Award input) { return input.getId(); } }); // 返回奖品名称列表 ImmutableList awardNameList = from(awardIdList).transform(new Function() { @Override public String apply(Integer input) { return idAwardMap.get(input).getName(); } }).toList(); System.out.println(awardNameList);
代码不够简洁, 看的时候还是容易被这些匿名内部类干扰,虽然写的时候也很流畅。优点是返回的集合都是不可变的。
List uwList = Lists.newArrayList(new UserWinning(userId, 1), new UserWinning(userId, 2), new UserWinning(userId, 3), new UserWinning(userId, 1)); //提取awardIdList List awardIdList = extract(uwList, on(UserWinning.class).getAwardId()); System.out.println(awardIdList); List awardList = Lists.newArrayList(new Award(1, "奖品一"), new Award(2, "奖品二"), new Award(3, "奖品三")); //List -> Map Map idAwardMap = index(awardList, on(Award.class).getId()); //得到奖品名称列表 List nameList = convert(awardIdList, new Converter() { @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
?
Our project uses Gauva
I have never used lambdaJ, but I feel that guava and lambdaJ cannot be compared in this way. The former has many convenient tools, and functional interfaces are only part of them.
Even if we use java8, we still use guava to simplify our development.
Finally, lambda is great.
Lambdaj is different from the native Lambda of java8 after all. If your project will be migrated to java8 in the future, you should carefully consider using LambdaJ.