tangara goh

Ranch Hand
+ Follow
since Dec 27, 2009
Merit badge: grant badges
I am a beginner in Java and hope to pick up the trade secrets so that I can move on to Android development
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by tangara goh

I checked page 125 of that book written by Urma Fusco and Mycroft and google many times but cannot find anything hence this posting.

This disturbing "You" is not helpful at all but i guess now i know i just have to grab the method reference from the Object in this case is it s method that is coming from FizzBuzz the class object.

2 weeks ago

Ira Go wrote:Tangara, you need to look at the whole pipeline then it will make sense. It probably starts with an IntStream stream. The mapToObj() method will infer the type from the function call as String and produce Stream<String>.

is function call refers to the method that is written ?

But, there is no explicit infor that the Type is referring to the className at all...it seems that one needs to learn through experience like this.
2 weeks ago

Stephan van Hulst wrote:The U doesn't refer to a class object. It refers to a type. In this case, it refers to the String type.

U is called a "generic type parameter". It's like a variable, but instead of holding a value like a primitive or object, it holds a type reference.

You can tell that the mapToInt() method has a generic type parameter because it is listed in front of the method, using angular brackets, like this: <U>

So who decides the value of this parameter? Well, just like with an ordinary method parameter, the caller of the method decides what type U stands for.

By calling the method with the method reference FizzBuzz::getStringForNum, you've decided that U refers to String, because mapToObj() expects a function that converts an int to a U, and you've passed it a reference to a method that converts an int to a String.

Thanks.  I am not sure who designed this but it doesn't really make sense to me as I can't really link the class to a String type ...... I wonder how others know .....
2 weeks ago
Please note "U" is not allowed so i have no choice but to changed it to YOU
2 weeks ago
I refer to the below  FizzBuzz code

I would like to seek help on understanding for the

how does one knows to use FizzBuzz before getting the method reference getStringForNum can be done because most of the tutorials i have read uses String, whatever it wants the map to do the transformation...

And when I look up the Oracle tutorial -

<U> Stream<U> mapToObj(IntFunction<? extends YOU> mapper)
Returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream.

This is an intermediate operation.

Type Parameters:
  ' YOU - the element type of the new stream'

I do not quite get how


is referring to the class object ?

I hope someone can explain things as even the book Modern Java In Action by does not even contain an example like that and I trolled the whatever tutorials out there, there is no single example like this for reference...it makes it difficult for people like me.

2 weeks ago
hi guys,

I refer to page 101 of Modern Java in Action cos I don't know whom to ask and i can't find the book forum in manning site, since I read that Campbell mentioned this book is good.

So, under 5.2.1, it shows how to use filter :

and then it said

the initial list was already sorted on the no of calories !

is the above code referred as the initial list ?

and then

Instead you could use :

my 2nd question is : should we use takeWhile instead of filter in code, esp in big stream of elements ?

Thanks again for your help.
4 weeks ago
Please ignore the above.  I got it finally.  
List<Person> copy = persons.stream()
                .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

System.out.println("what we have now is " + List.of(copy));

Note: the oracle's site using map instead of stream() really stumbled me...or is there something I did not comprehend about that part that uses
1 month ago

Campbell Ritchie wrote:
The variable stream is a notional variable meaning any sort of Stream, irrespective of how you created it. It is of course, a Stream<T>, and the compiler “knows” how to choose all the other generic type parameters for the rest of the Stream's progress.

I am not getting it,  So I tried out the example using the class Person.  it dosn't work out...as in it refuse to transform the Object to String and then collect the elements from the Person in the static list and make it into a new one...I am not comprehending the functions at all.

1 month ago
I read the Oracle site but there is this part :
Or we could use a parallelizable collect form:

    ArrayList<String> strings = stream.collect(() -> new ArrayList<>(),
                                               (c, e) -> c.add(e.toString()),
                                               (c1, c2) -> c1.addAll(c2));

or, pulling the mapping operation out of the accumulator function, we could express it more succinctly as:

    List<String> strings = stream.map(Object::toString)
                                 .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

what is stream here ?
1 month ago

I am starting to revise Java 8 and then I come across this code from https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps

but it can accomplish with the following :

I am terribly confusing....
1 month ago

Stephan van Hulst wrote:All three of your solutions have issues.

Your first modified push() method throws the wrong exception type.

Your second one throws an exception when instead, it's supposed to return false.

Your third solution throws an exception when instead, it's supposed to increase the capacity of the existing stack.

Yes.  Silly me on not reading the question carefully.  I guess I should also did a test ...

I am puzzled about the capacity that part.

If there is no capacity  stated in the constructor, how will the stack know that it's full ?

Or this class is a 'utility' class that is to be made use of with other List like ArrayList, LinkedList, or Tree(am not sure about this one as I haven't go deep into it yet)
1 month ago
Actually, I changed the push name for convenience sake.

Sorry I thought you could tell.

So, basically, I created 3 solutions in the same class.

Please ignore the comment, as I was first struggling how to push the element to the array without an index first.

So, the thing is I check with ai.  I was told that the exceptions should be thrown at the peek() and pop() and I thought I better hear the experts' opinion.
1 month ago
I refer to this exercise : https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html#zz-8.

the code given is :

and the requirements ;


   Modify the push() method to throw an IllegalStateException if the stack is full.
   Modify the push() to return true if the operation is successful, or false otherwise.
   Modify the push() to increase the capacity by reallocating another array, if the stack is full.

I just want to find out if the way i did my code that try to modify the push method is correct or not cos I will not write to the author, after I did not get any reply at all.

Thanks alot alot.

1 month ago

Campbell Ritchie wrote:

tangara goh wrote:. . . can i sum it up that we need to use compareTo for set, and we won't go wrong ?


I think you couldn't, not even if I understood the whole of that sentence. What have sets got to do with it?

ok. then what does this sentence mean from Oracle site :

This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals.

maybe my set is different from the sets ?
1 month ago

Campbell Ritchie wrote:Note that case‑insensitive ordering of Strings would give the same problem as above: compareTo() not consistent with equals().

can i sum it up that we need to use compareTo for set, and we won't go wrong ?
1 month ago