I am using Java 8 stream Iteration with a variable that should be used in other classes also. So I have used the below code.
AtomicBoolean bool = new AtomicBoolean(true);
public void testBool(){
list.stream().forEach(c->{
if( c.getName() != null){
bool.set(true);
}
});
}
public void test(){
if(bool.get()){
System.out.println("value is there");
}
}
But I heard like using the Atomic Object will be a performance hit sometimes. Is there any alternate approach to use the variables outside the forEach block with Java 8 usage? Without this am getting the error as a variable should be a final or effectively final error.
Please help me to resolve this issue.
Thanks in advance.