どうも、ちょげ(@chogetarou)です。
Map(マップ)が空かどうか判定する方法を紹介します。
方法

Map(マップ)が空チェックするには、isEmpty()を使います。
具体的には、「map.isEmpty()
」のように、MapからisEmpty()を呼び出します。
map.isEmpty()
上記のisEmtpy()は、呼び出したMapが空ならば「true」、空でなければ「False」を返します。
使用例
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Map<String, Integer> numbers = new HashMap<String, Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", null);
numbers.put("four", 4);
numbers.put("five", null);
Map<Integer, Integer> empty = new HashMap();
System.out.println(numbers.isEmpty());
System.out.println(empty.isEmpty());
}
}
出力:
false
true
コメント