2、JAVA8 之 Optional 线上应用

背景

  接着《1、Optional 应用》的基础,我们直接来看下线上的实际应用。

线上示例

1、案例一:循环内部判空

 原始代码

List<ConstantScoreQueryBuilder> validBuilderList = Lists.newArrayList();

for (ConstantScoreQueryBuilder queryBuilder : nameMtBuilder) {
  if (queryBuilder != null) {
    validBuilderList.add(queryBuilder);
  }
}

for (ConstantScoreQueryBuilder queryBuilder : tagBuilderList) {
  if (queryBuilder != null) {
    validBuilderList.add(queryBuilder);
  }
}

if (categoryBuilder != null) {
  validBuilderList.add(categoryBuilder);
}
if (nerEntityBuilder != null) {
  validBuilderList.add(nerEntityBuilder);
}

 Optional改造代码

List<ConstantScoreQueryBuilder> validBuilderList = Lists.newArrayList();

nameMtBuilder.forEach(nameBuilder -> Optional.ofNullable(nameBuilder).ifPresent(validBuilderList::add));
tagBuilderList.forEach(tagBuilder -> Optional.ofNullable(tagBuilder).ifPresent(validBuilderList::add));
Optional.ofNullable(categoryBuilder).ifPresent(validBuilderList::add);
Optional.ofNullable(nerEntityBuilder).ifPresent(validBuilderList::add);

2、案例二:循环外判空

 原始代码

String query = recallInfoCarrier.getQuery();
String originQuery = recallInfoCarrier.getOriginQuery();
List<String> correctQueryList = recallInfoCarrier.getCorrectQueryList();
List<String> querySynonymsList = recallInfoCarrier.getQuerySynonyms();
List<String> rewriteQueryList = recallInfoCarrier.getQueryRewrites();
Map<String, Double> reorderSearchWords = Maps.newLinkedHashMap();
reorderSearchWords.put(query, 10.0);
reorderSearchWords.put(originQuery, 10.0);

if (CollectionUtils.isNotEmpty(correctQueryList)) {
  for (String correctQuery : correctQueryList) {
    reorderSearchWords.put(correctQuery, 9.0);
  }
}
if (CollectionUtils.isNotEmpty(querySynonymsList)) {
  for (String synonyms : querySynonymsList) {
    reorderSearchWords.put(synonyms, 8.0);
  }
}
if (CollectionUtils.isNotEmpty(rewriteQueryList)) {
  for (String rewrite : rewriteQueryList) {
    reorderSearchWords.put(rewrite, 7.0);
  }
}
for (Map.Entry<String, Double> entry : searchWords.entrySet()) {
  if (reorderSearchWords.get(entry.getKey()) != null) {
    continue;
  }
  reorderSearchWords.put(entry.getKey(), 6.0);
}
recallInfoCarrier.setSearchWords(reorderSearchWords);

 Optional改造代码

Map<String, Double> reorderSearchWords = Maps.newLinkedHashMap();
reorderSearchWords.put(recallInfoCarrier.getQuery(), 10.0);
reorderSearchWords.put(recallInfoCarrier.getOriginQuery(), 10.0);

Optional.ofNullable(recallInfoCarrier.getCorrectQueryList()).orElse(Lists.newArrayList()).forEach(correctQuery -> reorderSearchWords.put(correctQuery, 9.0));
Optional.ofNullable(recallInfoCarrier.getQuerySynonyms()).orElse(Lists.newArrayList()).forEach(synonymsQuery -> reorderSearchWords.put(synonymsQuery, 8.0));
Optional.ofNullable(recallInfoCarrier.getQueryRewrites()).orElse(Lists.newArrayList()).forEach(rewriteQuery -> reorderSearchWords.put(rewriteQuery, 7.0));

searchWords.forEach((kQuery, score) -> reorderSearchWords.putIfAbsent(kQuery, 6.0));

recallInfoCarrier.setSearchWords(reorderSearchWords);

3、案例三:三目运算赋值 

import java.util.Optional;
 
public class Java8Tester {
   public static void main(String args[]){
      Java8Tester java8Tester = new Java8Tester();
      Integer value1 = null;
      Integer value2 = new Integer(10);
        
      // Optional.ofNullable - 允许传递为 null 参数
      Optional<Integer> a = Optional.ofNullable(value1);
        
      // Optional.of - 如果传递的参数是 null,抛出异常 NullPointerException
      Optional<Integer> b = Optional.of(value2);
      System.out.println(java8Tester.sum(a,b));
   }
    
   public Integer sum(Optional<Integer> a, Optional<Integer> b){
      // Optional.isPresent - 判断值是否存在
      System.out.println("第一个参数值存在: " + a.isPresent());
      System.out.println("第二个参数值存在: " + b.isPresent());
        
      // Optional.orElse - 如果值存在,返回它,否则返回默认值0
      Integer value1 = a.orElse(new Integer(0));
        
      //Optional.get - 获取值,值需要存在
      Integer value2 = b.get();
      return value1 + value2;
   }
}

 

posted @ 2021-04-18 22:08  星火燎原智勇  阅读(162)  评论(0编辑  收藏  举报