last day (15 days later) » 

6:20 AM
Hi @ernest_k
 
Hello
How are you?
 
I am fine'
Happy new Year :)
 
Thanks! Happy new year too
 
I have a doubt about making a variable as final while I need it inside the lambda and want to manipulate it.
what is the rescue?
as I want a variable to be manipulated inside lambda, I cannot use any other than a final.
 
Well, if you're sure that the change won't have negative effects, you can use a mutable reference.
 
6:24 AM
while I can do a escape is that I can make it static
mutable reference?
 
Like

AtomicReference<String> modifiableVariable = new AtomicReference<>();

...(() -> modifiableReference.set("new String"))...


String afterLambdaValue = modifiableVariable.get()
Let me find a link to a question with the correct answer
 
Ahh...I got it.
Well , I have a sub doubt
Kindly look at the code done by me:
public class TestClass {
static int count = 0;
static int t = 0;
public static void main(String[] args) {
int[] A = {1 ,2, 3 ,21, 7, 12, 14, 21};
int k = 4;
Arrays.sort(A);
IntStream.rangeClosed(0,A.length-1).boxed().map(x->{
if(!(A[t]+k>=A[x])){
count++;
t=x;
}
return count;
}).collect(Collectors.toList());
System.out.println(count+1);
}
}
I am sure in this
that there is no multi thread environment
shall using variables count and t as static good idea
?
 
You can check this answer: stackoverflow.com/a/30026897/5761558 for other techniques
 
provided I am sure that there is no multiple read and write conflict
one more approach I find is to use array.
int [] count = {0} is better and safer than static int count = 0;
Hmm...That link is very informative. Thanks
I learn from people like you, reading your aswers and learning from them.
Thanks once again
Bye :)
 
No problem. I see that main class/method has nothing strange, so there should be no problem with the static fields, in my opinion, but ideally one tries to avoid side-effects when using streams (as long as that's reasonable).
Thank you, and have a nice day
 

last day (15 days later) »