可変長引数とラムダを使って書いてみました。
java
1
2import java.util.ArrayList;
3import java.util.Arrays;
4import java.util.List;
5
6public class Mainx {
7 public static void main(String[] args) {
8 String value1 = null;
9 String value2 = null;
10 String value3 = null;
11 String value4 = "sample";
12 String value5 = null;
13 System.out.println("" + sample(value1, value2, value3, value4, value5));
14 System.out.println("" + sample("1"));
15 System.out.println("" + sample(null));
16 System.out.println("" + sample("1", null));
17 System.out.println("" + sample(null, null));
18 System.out.println("" + sample(null, "2", null));
19 System.out.println("");
20 System.out.println("" + sample("1", "2"));
21 System.out.println("" + sample("1", "2", "3"));
22 System.out.println("" + sample("1", "2", null));
23 }
24
25 static boolean sample(Object... args) {
26 if (args == null) {
27 return true;
28 }
29 List<Object> list = new ArrayList<>(Arrays.asList(args));
30 list.removeIf(val -> val == null);
31 return list.size() <= 1;
32 }
33}
実行例
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。