Comment calculer la valeur booléenne dans une liste de liste? Chaque élément d'une liste contient une liste imbriquée. Ci-dessous l'extrait de code
class ChildComponent {
List<ChildComponent> childComponents = new ArrayList<>();
Boolean notificationEnabled = Boolean.FALSE;
String name;
//respective Gettters/Setters
}
class ModelItem {
List<ChildComponent> components = new ArrayList<>();
Boolean alertUser;
}
ModelItem
avoir un list
des composants. Chaque composant des composants list
a un notificationEnabled
indicateur. En outre, chaque composant a une liste de ChildComponent
.
alertUser
doit être définitrue
dans les cas ci-dessous:
ChildComponent
dans le components
a notificationEnabled
défini surtrue
List<ChildComponent> childComponents
est notificationEnabled
défini surtrue
Donc, en gros, si l' un des Components
a notificationEnabled
mis à true
alors alertUser
devrait être réglétrue
Comment faire?
modelItem.components.stream().flatMap(a -> a.childComponents.stream())
ne fonctionne pas comme il le vérifie. Plus de détails dans l'extrait
Voici l'exemple de code:
public static void main(String[] args) {
ModelItem modelItem1 = new ModelItem();
modelItem1.components.add(createC1());
modelItem1.components.add(createC2());
updateAlert(modelItem1);
}
**// does not work. It only check childComponents**
private static void updateAlert(ModelItem modelItem) {
boolean anyMatchFilterPromptAtRun = modelItem.components.stream().flatMap(a -> a.childComponents.stream())
.anyMatch(b -> b.notificationEnabled.equals(Boolean.TRUE));
modelItem.alertUser = Boolean.valueOf(anyMatchFilterPromptAtRun);
}
private static ChildComponent createC1() {
ChildComponent c1_1 = new ChildComponent();
c1_1.name = "c1_1";
c1_1.notificationEnabled = false;
ChildComponent c1_2 = new ChildComponent();
c1_2.name = "c1_2";
c1_2.notificationEnabled = true;
// main C2
ChildComponent c1 = new ChildComponent();
c1.name = "C1";
c1.childComponents = Arrays.asList(c1_1, c1_2) ;
c1.notificationEnabled = false;
return c1;
}
private static ChildComponent createC2() {
ChildComponent c1_1 = new ChildComponent();
c1_1.name = "c1_1";
c1_1.notificationEnabled = false;
ChildComponent c1_2 = new ChildComponent();
c1_2.name = "c1_2";
c1_2.notificationEnabled = true;
// main C1
ChildComponent c1 = new ChildComponent();
c1.name = "C1";
c1.childComponents = Arrays.asList(c1_1, c1_2) ;
c1.notificationEnabled = false;
return c1;
}
Voici l'exemple de structure
{
"components": [
{
"name": "C1",
"notificationEnabled": false,
"childComponents": [
{
"name": "c1_1",
"notificationEnabled": false,
"childComponents": []
},
{
"name": "c1_2",
"notificationEnabled": true,
"childComponents": []
}
]
},
{
"name": "C1",
"notificationEnabled": false,
"childComponents": [
{
"name": "c1_1",
"notificationEnabled": false,
"childComponents": []
},
{
"name": "c1_2",
"notificationEnabled": true,
"childComponents": []
}
]
}
]
}
Il semble que vous deviez parcourir de manière récursive une hiérarchie de composants. Vous pouvez faire quelque chose comme ceci:
Stream<ChildComponent> recursiveWalk(List<ChildComponent> list) {
if (list.isEmpty())
return Stream.empty();
}
return Stream.concat(list.stream(),
recursiveWalk(list.stream()
.map(ChildComponent::childComponents)
.flatMap(Collection::stream)
.collect(toList())));
}
alertUser = recursiveWalk(components).anyMatch(ChildComponent::notificationEnabled);
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots