使用匹配属性更新列表中元素的属性 [英] Updating an attribute of an element in list with matching attribute

查看:106
本文介绍了使用匹配属性更新列表中元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java 8的新手。我有一个A类对象列表,其中A的结构如下:

I am new to Java 8. I have a list of objects of class A, where structure of A is as follows:

class A {
   int name,
   boolean isActive
}

现在我有一个A类元素L的列表,在该列表中我想要更新一个名为=test且具有inactive = false的元素。

Now I have a list of elements L of class A, in that list I want to update an element having name="test" with inactive=false.

I通过编写for循环和创建新列表,可以非常轻松地完成此任务。

I can do this very easily by writing a for loop and creating a new list.

但是如何使用Java 8流API进行此操作?

But how would I do that using Java 8 stream API?

推荐答案

你可以这样做。

L.stream()
.filter(item-> item.getName().equals("test"))
.forEachOrdered(a -> a.setActiv(false));

我认为名称的数据类型应为 String 在你的问题中不是 int

I believe data type of name should be String not int in your question

这篇关于使用匹配属性更新列表中元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆