Home  >  Article  >  Java  >  Don’t return null in java development

Don’t return null in java development

WBOY
WBOYforward
2023-05-04 23:49:16909browse

Do not return null

Counterexample

Don’t return null in java development

##Positive example

Don’t return null in java development

When calling methods elsewhere, avoid unnecessary null pointers

optional null

//获取子目录列表
public List<CatalogueTreeNode> getChild(String pid) {
            if (V.isEmpty(pid)) {
            pid = BasicDic.TEMPORARY_DIRECTORY_ROOT;
        }
        CatalogueTreeNode node = treeNodeMap.get(pid);
 
        return Optional.ofNullable(node)
                .map(CatalogueTreeNode::getChild)
                .orElse(Collections.emptyList());
    }

The above is the detailed content of Don’t return null in java development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete