# Bart Boersma

Ranch Hand
since Nov 29, 2016
Cows and Likes
Cows
3
In last 30 days
1
Total given
0
Likes
3
0
Total given
20
Given in last 30 days
1
Scavenger Hunt
Ranch Hand Scavenger Hunt
Greenhorn Scavenger Hunt

## Recent posts by Bart Boersma

Ah I should have noticed taking into account I did OCA last year. That shows how fast knowledge gets rusty! Thanks all.
2 weeks ago
Hi all,

Im learning about Stream and have the following question. Why is the first peek() method not destructive, and thus flatMap() and forEach() are executed. And in the second stream the count() method is not reached because the peek() is destructive?

2 weeks ago
I have not been able to identify the logic error ..
2 weeks ago
Yep I get it: say integer1 is 1000 and integer2 is -1000 the comparator would return 0 meaning the integer values are the same which they are clearly not. Just one question: where does PS stand for?
2 weeks ago
2 weeks ago
Hi Piet,

My comparators are indeed both the same. So how does the min() method decide to take the minimum and the max() method to take the maximum?

Regards
2 weeks ago
Hi all,

Im learning for the OCP exam and came across min and max method on stream. Both methods take the exact same operator but still they yield different results. I do not understand why. In the below code im comparing integers the exact same way. Obviously, I am missing something because from my (incorrect) perspective it seem exact similar methods and therefore would seems two methods are just given different names. The fact that the results are different shows that I am incorrect, but what am I missing?

2 weeks ago
Hi guys,

I am reading the OCP study guide by Scott Selikoff. On page 150 they briefly talk about a way to compare multiple fields using the Comparator<T> interface. The code snippet below is quoted from the book.

I understand lambda's until a certain extent so I get that the upper code can be written out in the following code

So I get that the Comparator.comparing(Function<T, R> f) method returns a Comparator object.

1) But what does it do behind the scene's?
2) What does the funcational interface Function (and apply method) do behind the scene's (I get that it order the String values specified in te getSpecies() method but how does it work?

Bart
3 weeks ago
My question is regarding the fact that an ArrayDeque does not accept null values. The OCP study guide states the following: "You can’t put null in an ArrayDeque because methods like poll() use null as a special return value to indicate that the collection is empty. Since null has that meaning, Java forbids putting a null in there. That would just be confusing." I understand that but then why is it allowed for a LinkedList to accept null values. I would expect a LinkedList to reject null values as well because it also uses methods like poll()?

Can someone explain me this difference?

Bart
3 weeks ago
I guess you are aiming at the method parameter of the equals method that takes the object type Lion rather than the object type Object which would turn it from an overloaded equals method to an overridden equals method which is more common

Am I right?
1 month ago
Clear and straightforward answer, thank you!
1 month ago
Hi,

Question about overriding the hashCode() method. The OCP study guide book states (page 19 just above the real world scenario) that the following situation is not legal:

The book states: it is not legal because it uses more variables than the equals() method which looked like:

So from what I understand it is not legal to use the age variable in the hashCode() method because it is not used in the equals method.

My question is: why is this 'not legal'? I tested it and it does compile but what do they mean with it is not legal?
1 month ago
Last thing I want to give you is that this was the first Java book I read too and I can remember that these longer programs where hard to read. Dont bother too much understanding every detail of this code. Some parts, such as the cast: (int), are not yet explained and will be clarified later on.
7 months ago
Hi Gaurav,

Here are a few takaways which might help you understand the code:
1) The game firstly creates three instance variables (Player p1; Player p2; Player p3) and then assigns three objects to them (p1 = new Player()). This could also be done in one line namely: Player p1 = new Player(). This variant you have probably seen before. Each player (p1, p2 and p3) represent a person guessing a number.

2) Next booleans (true or false) are declared (p1isRight etc.). As the name suggest this boolean holds whether a player guessed the right number.

3) int targetNumber = (int) (Math.random() * 10) is a way of randomly generating a number between 0 and 9. Math.random() generates a number between 0 and 1. Timing it by 10 makes sure the number is between 0 and 9. The (int) part is called a cast which will be explained later on in the book. The simple explanation is: Math.random() is not the same variable type (byte, short, boolean, int) as the the variable targetNumber is. Math.random() returns a double (variable type) where the variable targetNumber is an int that is why we need a cast.

4) while(true) basically says makes this run forever. It basically says while(true == true) which is always true. The reason why it does not loop infinitely is because somewhere there is a break statement (sidenote: I think this way of coding is questionable).

5) The class player which we created 3 of has the method guess. Therefore every variable that points to a player class (p1, p2, p3) can call the guess() method. Within the guess method a random number is generated between 0 and 9 just as with the variable targetNumber. Then this generated number is set to the number variable of each class.

6) Now that each player has generated a random number which is saved in the number variable we can set this to the previously initialized variables guessp1, guessp2 and guessp3. Since the number variable is different for each variable that points to the player class we can access this by taking the player class variable (p1, p2 and p3) and access their number (p1.number, p2.number and p3.number).

7) Next up we check whether their guess (guessp1, guessp2, guessp3) equals the randomly generated targetNumber. We do this for each guess seperatly therefore we have 3 if statements and not an:

8) In Java if(p1isRight){....} is the same as if(p1isRight == true). Similar to if(!p1isRight) is the same as if(p1isRight == false). Besides that the || is an or operator. Thus the p1isRight || p2isRight || p3isRight checks whether p1isRight == true OR p2isRight == true OR p3isRight == true.

9) In the if statement we basically print out a few lines and break the loop (the while(true) loop earlier referred to).

Hope this helps!

Bart

This is the code my answer is based upon:
7 months ago
This is clearing the sky!

So from what I understand, passing the arraylist into the method creates a new reference to the object (so now there are 2 references to the object: the original reference and the method parameter reference). Therefore updating the method parameter reference will affect the object.
1 year ago