Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用stream为hashmap实现isEmpty()?_Java_Hashmap_Java Stream - Fatal编程技术网

Java 如何使用stream为hashmap实现isEmpty()?

Java 如何使用stream为hashmap实现isEmpty()?,java,hashmap,java-stream,Java,Hashmap,Java Stream,我正在实现自己的hashmap,尽可能多地使用流。 我想不出isEmpty()的正确语法 我尝试过多种形式的: return buckets.entrySet().stream().forEach(List::isEmpty()); 以下是代码的一部分: public class HashMap<K, V> implements Map<K, V> { private static final int DEFAULT_CAPACITY = 64; p

我正在实现自己的hashmap,尽可能多地使用流。 我想不出isEmpty()的正确语法

我尝试过多种形式的:

 return buckets.entrySet().stream().forEach(List::isEmpty());
以下是代码的一部分:

public class HashMap<K, V> implements Map<K, V> {

    private static final int DEFAULT_CAPACITY = 64;
    private List<List<Entry<K, V>>> buckets;
    private int modCount = 0;
    private KeySet keySet;
    private EntrySet entrySet;
    private ValuesCollection valColl;

    //CTOR
    public HashMap() {
         buckets = new ArrayList<>(DEFAULT_CAPACITY);

         for (int i = 0; i < DEFAULT_CAPACITY; ++i) {
            buckets.add(i, new LinkedList<Entry<K, V>>());
         }
    }
}
对于isEmpty()。

您可以使用以下选项:

public boolean isEmpty() {
    return buckets.stream().allMatch(List::isEmpty);
}

如果
bucket
为空或所有子列表为空,则返回
true

谢谢你,我会详细介绍allMatch()。你也可以看看
anyMatch()
noneMatch()
public boolean isEmpty() {
    return buckets.stream().allMatch(List::isEmpty);
}