でoptionalsを使用して最初のないnull値を取得しますベストな方法は、我々はこのようなコードを持っているJavaの
String tempDir = SwingInstanceManager.getInstance().getTempFolderPath(clientId);
if (tempDir == null) {
tempDir = System.getProperty(Constants.TEMP_DIR_PATH);
if (tempDir == null) {
tempDir = new File(System.getProperty("java.io.tmpdir")).toURI().toString();
}
}
それは私がこのように記述しますのみ2値だったので、もし私は、括弧を削除する:
String tempDir = Optional.ofNullable(SwingInstanceManager.getInstance().getTempFolderPath(clientId)).orElse(System.getProperty(Constants.TEMP_DIR_PATH));
しかし3+値のため、このようなチェーンを書くには?(orElse呼び出しで2番目のオプションを使用してwithount)
それがコードの量を増加させ、代わりにそれを減少させる:( – maxpovver
まあ、本当にそれが(ちょうど1行 'list.streamです)。フィルタリング(I =>私!= null).findFirst() '残りはここにあるのでスタンドアローンとして動作します。 – Aaron