guava实践优雅代码(一)

1.多关键字排序

(1)不优雅实现(wmpoutput线上代码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static class  implements Comparator<InventoryInputDTO> {
public int compare(InventoryInputDTO o1, InventoryInputDTO o2) {
if(o1.getBdd().getSourceDocType().getCode() == o2.getBdd().getSourceDocType().getCode()) {
if(o1.getBdd().getSourceDocId().longValue() == o2.getBdd().getSourceDocId().longValue()) {
if(o1.getBdd().getSourceDetailDocId().longValue() == o2.getBdd().getSourceDetailDocId().longValue()) {
if(o1.getItemBatchId() == null || o1.getItemBatchId() == null) {
return 0;
} else if(o1.getItemBatchId().longValue() == o2.getItemBatchId().longValue()) {
if(o1.getCabinetId() == null || o1.getCabinetId() == null) {
return 0;
} else {
return o1.getCabinetId().intValue() - o2.getCabinetId().intValue();
}
} else {
return o1.getItemBatchId().intValue() - o2.getItemBatchId().intValue();
}
} else {
return o1.getBdd().getSourceDetailDocId().intValue() - o2.getBdd().getSourceDetailDocId().intValue();
}
} else {
return o1.getBdd().getSourceDocId().intValue() - o2.getBdd().getSourceDocId().intValue();
}
} else {
return o1.getBdd().getSourceDocType().getCode() - o2.getBdd().getSourceDocType().getCode();
}
}
}

(2)优雅实现(利用guava的ComparisonChain实现多关键字比较)

1
2
3
4
5
6
7
8
9
10
11
public static class  implements Comparator<InventoryInputDTO> {
public int compare(InventoryInputDTO o1, InventoryInputDTO o2) {
return ComparisonChain.start()
.compare(o1.getBdd().getSourceDocType().getCode(), o2.getBdd().getSourceDocType().getCode())
.compare(o1.getBdd().getSourceDocId().longValue(), o2.getBdd().getSourceDocId().longValue())
.compare(o1.getBdd().getSourceDetailDocId().longValue(), o2.getBdd().getSourceDetailDocId().longValue())
.compare(o1.getItemBatchId(), o2.getItemBatchId())
.compare(o1.getCabinetId(), o2.getCabinetId())
.result();
}
}

2.繁琐的for循环遍历(如过滤掉状态为-1的对象)

2.1 集合过滤

(1)不优雅实现(wmpoutput线上代码)

1
2
3
4
5
6
7
8
9
10
11
12
13

* 过滤废弃的包裹
* @param listVO
*/

private void removeUselessPackage(List<RvPackageVO> listVO) {
List<RvPackageVO> result = Lists.newArrayList();
for (RvPackageVO packageVO : listVO) {
if (!packageVO.getStatus().equals(-1)) {
result.add(packageVO);
}
}
listVO = result;
}

(2)优雅实现(利用guava的过滤器过滤集合中不符合条件的元素)

1
2
3
4
5
Collection<YitongObject>  filterCollection = Collections2.filter(list, new Predicate<YitongObject>(){ 
@Override
public boolean apply(YitongObject obj) {
return !obj.getStatus().equals(-1);
}});

2.2 集合中对象转换

(1)不优雅实现(wmpevent线上代码)

1
2
3
for(PackageEventLogDTO eventDTO:eventDTOList){
eventDTO.setStatusText(SaleOutEventConstant.PackageStatus.getEnumValue(eventDTO.getStatus()));
}

(2)优雅实现

1
2
3
4
5
6
7
Collection<RvPackageVO>  formatCollection = Lists.transform(list, new Function<RvPackageVO,RvPackageVO>(){
@Override
public RvPackageVO apply(RvPackageVO input) {
input.setStatusStr(RvNoticeStatus.PACKAGE.findLabel(input.getStatus()));
return input;
}
});

3.集合

(1)new一个集合

1
2
3
Map<String, Collection<ItemBarcodeDTO>> result = new HashMap<String, Collection<ItemBarcodeDTO>>();

List<Foo<Bar, Baz>> list = new ArrayList<Foo<Bar, Baz>>();

(2)利用guava的集合类

1
2
3
Map<String, Collection<ItemBarcodeDTO>> result =Maps.newHashMap();

List<Foo<Bar, Baz>> list = Lists.newArrayList();

4.
ArrayList还给我们提供了将底层数组的容量调整为当前列表保存的实际元素的大小的功能。它可以通过trimToSize()方法来实现。该方法可以最小化ArrayList实例的存储量。