Florian Jedamzik

Ranch Hand
+ Follow
since Nov 13, 2016
Cows and Likes
Cows
Total received
3
In last 30 days
0
Total given
0
Likes
Total received
6
Received in last 30 days
0
Total given
24
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Florian Jedamzik

Ah I see!;)

Thank you Jeanne and Pawel;)
Hi guys;)

Lately more and more questions pop up...

For example I don't get why this Lines of Code actually compile:



why does line //x compile? I always thought forEach() expects
a Consumer, not a Function?

Then why does it work? Here, forEach() returns x+10;
and a Consumer never returns anything...so
I'm confused:)

Kind regards
Florian
Thank you both!;)

Now I think I got it;)
Hi;)

I am more and more finishing my Preparation for the OCP Exam and I am very
confused with this Code:



Why does the second println() Statement print out 3?!?!

Shouldn't it get the max Value?

Kind regards
Florian
Ah, now I see;)

Thank you Roel, that really helped me a lot!;)

Hey there;)

My Last question before the exam;)

I am currently reviewing every Possibility that I
could encounter in the exam, and so I stumbled
upon those two:

Why does this return 11111 and not 12345?

Stream.iterate(1, x -> x++).forEach()System.out::println);

why do I need the the pre-Increment Operator here?

The next one is:

Why does



work with this:
??

I thought only partitioningBy() would take a Predicate and
groupingBy() would take a Function?

Kind regards
Florian
So, to conclude;

1. compose() runs
2. func runs
3. andThen() runs (last)

Kind regards
Flo

Henry Wong wrote:

Please explain what you mean by "mixed up"..... and also... Keep in mind that there are two threads here. The one thread in the single thread executor, and the main thread which is running the code in the main() method.

Henry



By that I mean it can sometimes output 100 100, but also
100 47 or 10 86 or 100 99 etc...

Ok, I see...then the main Method runs this Code Snippet
independently from the SingleThreadExecutor:



And that's why the ouput could be "mixed up"?
Did I get this right?;)

Kind regards and thank you for your answer;)
Florian
Hi;)

As I am currently finishing my preparationfor the OCP Exam, I
am working myself through Threads (the hardest Chapter ;) ).

I don't quite understand why the Output cannot be determined
exactly here:



I thought a newSingleThreadExecutor does everything sequentially?
So why would the Output be "mixed up?"

Kind regards
Florian

João Victor Gomes wrote:
Taking the following code separately:

It means that it will initialize all the instance elements and constructor in order to finish this static initializer, so the other static elements can be initialized as well. Is that right?

Could anyone help me with this one?

Thanks!



Hi Victor!;)

I think it's exactly as you stated. The static Initializers
run in the coded Order. So when running



The Object gets created and Its instance Initializers are run first,
and then the Constructor. After that, the static Block

( )

has finished. Then the next static Initializer runs.

You play a littel bit by commenting out parts of the code/the
initializers/Contructors and see what happens;)

Kind regards
Florian

Jesper de Jong wrote:

Florian Jedamzik wrote:What is going on here is:

in the Function "func2" the Method that acts up First, is compose(),
after it comes "andThen(). And last but not least func gets to
run, am I right here?


This is what happens:

In the following line:

1. The method 'andThen' is called on 'func' with a lambda expression as its argument. This returns a value, which is another Function object.
2. The method 'compose' is called on the Function that was returned in step 1. This returns another Function object.

When you then call func2.apply("4") then the following happens:

3. You are calling 'apply' on the Function object that was returned by the 'compose' method in step 2. This method first executes the lambda expression that was passed to it, which is in this case: (String s) -> Integer.parseInt(s) - so the "4" that you pass to apply() is converted to an int.

4. The 'compose' method then calls 'apply' on the Function object that it was called on in step 2 - so it calls 'apply' with the output of the lambda (the integer 4) on the Function object that was returned in step 1, which was created by the 'andThen' method.

5. The Function object returned by the 'andThen' method does this: it first calls the 'apply' of the Function object on which it was called itself, and then it calls the lambda expression that you passed to it on the result of the first 'apply' (so, this is exactly the opposite order as what 'compose' does). So: func.apply() is called with the integer 4. This translates the integer 4 back into a string "4". And then the lambda (String s) -> s+"2" is called, which concatenates strings "4" and "2", returning "42".




Thank you for the exhaustive and exact explanation Jesper;)

Stephan van Hulst wrote:That's incorrect. andThen() is performed first, and then compose() is performed, because compose() is to the right in the method chain.

First you have func, which is a Integer -> String. Then you perform andThen(), which turns it into another Integer -> String. Finally you perform compose(), which turns it into a String -> String.

Why would compose() be executed first?



Thank you for the Explanation;)

I thought compose() executes first by default every time...didn't think
about the order of my code that much...;)

Kind regards
Florian
Hi all;)

I have come across a tricky Function (at least for me), that I don't
quite understand...

It's this here:




What is going on here is:

in the Function "func2" the Method that acts up First, is compose(),
after it comes "andThen(). And last but not least func gets to
run, am I right here?

Because of this what I don't understand is the following:

If compose() converts a String to an int-Value, how
can andThen() still compile? As specified here, it does
also expect a String Value (String s) but it gets an int,
because the compose() method converted the String to in
before it reached the anThen() method...

This is what I don't get...or did I oversee something here?

Kind regards:)

Florian
Haha ok thank you!;)
Hey it's me again;)

I'm currently trying out the Questions in the OCP Study Guide,
and I encountered an interesting question, where i "modified"
the Code a little bit.

See it here:




So I just coded a ScheduledExecutorService with a ThreadPool of 5.
Then I wanted tasks to be submitted every Second.

I am also currently explaining the Code to myself,
and I just wanted to know if I get it all right here.

To answer what is going on here, I'd say,
that there might be a deadlock at Runtime,
when one Thread acquires a lock on o1 in the
ScheduledFuture f1
and another Thread acquires a lock at the same
time on o2 in the ScheduledFuture f2.
This would be a Deadlock, am I right?


Kind regards
Florian