简化了匿名委托的使用,让你让代码更加简洁,优雅
自定义一个bean
package com.zyxr.vip.mall.goods.bean;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class GoodsRule {
public Long ruleId;
public Long goodsId;
public Integer firstType;
public Integer secondType;
public String secondValue;
public GoodsRule(Long goodsId, Integer firstType, Integer secondType, String secondValue) {
this(firstType, secondType, secondValue);
this.goodsId = goodsId;
}
public GoodsRule(Integer firstType, Integer secondType, String secondValue) {
this.firstType = firstType;
this.secondType = secondType;
this.secondValue = secondValue;
}
}
测试类
@Before
public void init() {
log.info("begin to init data!");
ruleList = new ArrayList<>();
ruleList.add(new GoodsRule(1L, 1, 1, "3"));
ruleList.add(new GoodsRule(1L, 2, 1, "呵呵"));
ruleList.add(new GoodsRule(1L, 2, 2, "3"));
ruleList.add(new GoodsRule(1L, 3, 1, "1232"));
ruleList.add(new GoodsRule(2L, 1, 1, "3"));
ruleList.add(new GoodsRule(2L, 2, 1, "qqq"));
log.info("end to init data!");
}
@Test
public void testForEach() {
log.info("传统的遍历!");
for (GoodsRule rule : ruleList) {
log.info(gs.toJson(rule));
}
log.info("使用Lambda表达式遍历!");
ruleList.forEach(rule -> log.info(gs.toJson(rule)));
}
执行结果
INFO [VIPApp] 2018-08-11 16:51:50.125 [main] com.zyxr.vip.test.mall.AdvisorTest 252 - begin to init data!
INFO [VIPApp] 2018-08-11 16:51:50.125 [main] com.zyxr.vip.test.mall.AdvisorTest 260 - end to init data!
INFO [VIPApp] 2018-08-11 16:51:50.125 [main] com.zyxr.vip.test.mall.AdvisorTest 265 - 传统的遍历!
INFO [VIPApp] 2018-08-11 16:51:50.125 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 267 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 269 - 使用Lambda表达式遍历!
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:51:50.126 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO [VIPApp] 2018-08-11 16:51:50.127 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:51:50.127 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO [VIPApp] 2018-08-11 16:51:50.127 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:51:50.127 [main] com.zyxr.vip.test.mall.AdvisorTest 270 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
测试类
@Before
public void init() {
log.info("begin to init data!");
ruleList = new ArrayList<>();
ruleList.add(new GoodsRule(1L, 1, 1, "3"));
ruleList.add(new GoodsRule(1L, 2, 1, "呵呵"));
ruleList.add(new GoodsRule(1L, 2, 2, "3"));
ruleList.add(new GoodsRule(1L, 3, 1, "1232"));
ruleList.add(new GoodsRule(2L, 1, 1, "3"));
ruleList.add(new GoodsRule(2L, 2, 1, "qqq"));
log.info("end to init data!");
}
@Test
public void testTraditionSort() {
log.info("排序前:");
ruleList.forEach(rule -> log.info(gs.toJson(rule)));
//排序规则:按照goodsId降序
Collections.sort(ruleList, new Comparator() {
@Override
public int compare(GoodsRule o1, GoodsRule o2) {
return (int) (o2.goodsId - o1.goodsId);
}
});
log.info("排序后:");
ruleList.forEach(rule -> log.info(gs.toJson(rule)));
}
@Test
public void testLambdaSort() {
log.info("排序前:");
ruleList.forEach(rule -> log.info(gs.toJson(rule)));
//排序规则:按照goodsId降序
Collections.sort(ruleList, (GoodsRule o1, GoodsRule o2) -> (int) (o2.goodsId - o1.goodsId));
log.info("排序后:");
ruleList.forEach(rule -> log.info(gs.toJson(rule)));
}
执行结果
两个测试案例的结果一样
INFO [VIPApp] 2018-08-11 16:59:20.348 [main] com.zyxr.vip.test.mall.AdvisorTest 252 - begin to init data!
INFO [VIPApp] 2018-08-11 16:59:20.349 [main] com.zyxr.vip.test.mall.AdvisorTest 260 - end to init data!
INFO [VIPApp] 2018-08-11 16:59:20.349 [main] com.zyxr.vip.test.mall.AdvisorTest 280 - 排序前:
INFO [VIPApp] 2018-08-11 16:59:20.350 [main] com.zyxr.vip.test.mall.AdvisorTest 281 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:59:20.350 [main] com.zyxr.vip.test.mall.AdvisorTest 281 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO [VIPApp] 2018-08-11 16:59:20.350 [main] com.zyxr.vip.test.mall.AdvisorTest 281 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:59:20.350 [main] com.zyxr.vip.test.mall.AdvisorTest 281 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
INFO [VIPApp] 2018-08-11 16:59:20.350 [main] com.zyxr.vip.test.mall.AdvisorTest 281 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:59:20.350 [main] com.zyxr.vip.test.mall.AdvisorTest 281 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO [VIPApp] 2018-08-11 16:59:20.351 [main] com.zyxr.vip.test.mall.AdvisorTest 284 - 排序后:
INFO [VIPApp] 2018-08-11 16:59:20.352 [main] com.zyxr.vip.test.mall.AdvisorTest 285 - {"goodsId":2,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:59:20.352 [main] com.zyxr.vip.test.mall.AdvisorTest 285 - {"goodsId":2,"firstType":2,"secondType":1,"secondValue":"qqq"}
INFO [VIPApp] 2018-08-11 16:59:20.352 [main] com.zyxr.vip.test.mall.AdvisorTest 285 - {"goodsId":1,"firstType":1,"secondType":1,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:59:20.352 [main] com.zyxr.vip.test.mall.AdvisorTest 285 - {"goodsId":1,"firstType":2,"secondType":1,"secondValue":"呵呵"}
INFO [VIPApp] 2018-08-11 16:59:20.353 [main] com.zyxr.vip.test.mall.AdvisorTest 285 - {"goodsId":1,"firstType":2,"secondType":2,"secondValue":"3"}
INFO [VIPApp] 2018-08-11 16:59:20.353 [main] com.zyxr.vip.test.mall.AdvisorTest 285 - {"goodsId":1,"firstType":3,"secondType":1,"secondValue":"1232"}
Lambda表达式可以简化遍历和匿名内部类,能使代码更加的优雅。在ideal中使用匿名类会提示
Anonymous new Comparator
This inspection reports all anonymous classes which can be replaced with lambda expressions
Lambda syntax is not supported under Java 1.7 or earlier JVMs.
由此看出,官方也推荐使用Lambda表达式。但更大的应用场景是和Stream联合使用,将在下一篇中详细介绍。