为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 forEach 或 forEachValue 方法

如何使用 forEach 或 forEachValue 方法

慕容森 2023-06-28 15:51:00
我有一个ConcurrentHashMap<String, Document>,我必须调用一个方法来删除每个值的文件。Document 是将文档表示为文件的类。我在 eclipse 上发现了 forEach 和 forEachValue 方法(具有许多签名),但我不知道如何使用它们(有人可以向我解释一下吗?)。我可以调用 ConcurrentHashMap 方法:elements 方法获得Enumeration<V>Values 方法获取 aCollection<V>并因此获取迭代器哪些解决方案更好?
查看完整描述

3 回答

?
MYYA

TA贡献1868条经验 获得超4个赞

您可以使用 ,example 轻松迭代map键或值for each loop:


Map<String,Document> chm=new ConcurrentHashMap<String,document>();

//this for each loop will iterate over the values of the map

//to iterate over the keys you write: for(String str : chm.keySet())

for(Document doc : chm.values()){

   // code to delete the document 

   // example(if your Document exposes a delete method): doc.delete();

}

正如您在 a 中看到的,for each loop您首先声明对象的类型:String、int等Document...


第二部分是您的局部变量,例如在上面的情况下,文档将是Document您的每个变量map,您可以调用与它相关的任何方法。


第三部分是the :,你可以把它想象成“in”这个词


最后一部分是您想要迭代的内容,在上面的情况下它是 的map值


Enumeration 和 Iterator 接口的主要区别在于 Enumeration 只遍历 Collection 对象,在使用 Enumeration 进行迭代时不能对集合进行任何修改。其中 Iterator 接口允许您在遍历 Collection 对象时删除元素(使用该remove()方法)。


查看完整回答
反对 回复 2023-06-28
?
慕容708150

TA贡献1831条经验 获得超4个赞

forEach方法以 aBiConsumer作为参数,其中消费者的输入是映射中的键和值对:

void forEach(long parallelismThreshold, BiConsumer<? super K,? super V> action)

forEachValue方法以 aConsumer作为参数,其中消费者的输入只是映射中的值:

void forEachValue(long parallelismThreshold, Consumer<? super V> action)

在每种方法中,您可以定义文档parallelismThreshold中的说明:

这些批量操作接受parallelismThreshold 参数。如果估计当前地图大小小于给定阈值,方法将按顺序进行。使用 Long.MAX_VALUE 值会抑制所有并行性。

因此forEach,如果您需要键和值,请使用forEachValue,如果您只需要值,请使用:

例如

myMap.forEach(Long.MAX_VALUE, (key, document) -> deleteDocument(key, document));
...
public void deleteDocument(String key, Document d) { ... }

或者:

myMap.forEachValue(Long.MAX_VALUE, document -> deleteDocument(document));
...
public void deleteDocument(Document d) { ... }


查看完整回答
反对 回复 2023-06-28
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

如果您需要对 中的每个值执行某些操作ConcurrentHashMap,您有很多选择:

for (Map.Entry<String, Document> entry : map.entrySet()) {

    String key = entry.getKey();

    Document doc = entry.getValue();

    // code here

}


for (String key : map.keySet()) {

    // code here

}


for (Document doc : map.values()) {

    // code here

}

map.forEach((key, doc) -> {

    // code here

});


map.forEach(1, (key, doc) -> {

    // code here will run in parallel

});


map.forEachEntry(1, entry -> {

    String key = entry.getKey();

    Document doc = entry.getValue();

    // code here will run in parallel

});


map.forEachKey(1, key -> {

    // code here will run in parallel

});


map.forEachValue(1, doc -> {

    // code here will run in parallel

});

上面的示例使用了 lambda 表达式块,但是如果code here只是一条语句,您可以省略{}大括号和;语句终止符,并将所有内容保留在一行中,使其看起来更清晰,例如


map.forEachValue(Long.MAX_VALUE, doc -> DocUtil.deleteFile(doc));

或者使用方法参考:


map.forEachValue(Long.MAX_VALUE, DocUtil::deleteFile);

您更喜欢上述哪一个完全取决于您,部分取决于您是否需要key、doc或两者,以及是否需要并行处理。


删除文件时并行处理可能不会提高性能,因为这可能需要磁盘访问,但如果性能至关重要,您可以尝试并行运行并亲自看看这是否有帮助。


查看完整回答
反对 回复 2023-06-28
  • 3 回答
  • 0 关注
  • 164 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信