Help coderanch get a
new server
by contributing to the fundraiser

J Mohandos

Ranch Hand
+ Follow
since Oct 10, 2014
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by J Mohandos

I had an additional question on priority queue. So I thought instead of asking in separate thread I will ask in this thread itself.

Here in the below code, the list of persons needed to be sorted by using Collections.sort but for priority queue I did not have to call the sort method.

What is the reason for that?

3 years ago
How are the elements of the priority queue sorted according to natural order when it does not implement Comparable or Comparator interface internally?
3 years ago

Campbell Ritchie wrote:...but don't all subclasses of Number implement Comparable? In which case you can test whether the result from the comparison is <1, or >1, or 0.

No, the subclasses don't all implement Comparable. Can you parametrise that method with <T extends Number & Comparable<T>>? You would usually write Comparable<? super T>>, but in this instance there aren't any Comparable supertypes and you shouldn't subclass any of those types.
Don't use multiple ifs, nor mark the method void. Return the result of applying the ?: operator.



I have checked the following subclasses of Number(BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short) and all of them implement Comparable. So which subclass of Number does not implement Comparable?

Also I didn't understand this line "but in this instance there aren't any Comparable supertypes and you shouldn't subclass any of those types."

3 years ago

Piet Souris wrote:For the Integer it works because of unboxing. For BigIntegers that also extend Number, '<' is not defined.



Where can I find the information regarding which primitive types or wrapper classes support the less than (<) operator?

3 years ago
Hi All,

My question is on the below code.

Here in the main method, I am comparing 2 Integer objects and there is no compilation issue related to that.

But in the findMin method I have defined a generic type T extends Number and trying to compare the same, but it is giving compilation error.

Although I can use the compareTo method, my question why is the below code not working?

3 years ago
I am not saying display method decides the behavior of asList() method.

I am asking how did the call happen when there is a restriction on the type of value allowed in the list for the display method.
3 years ago
I tried with lower bounded wildcard also. Although it is working, I am unable to understand when I am using the super keyword how is it allowing object of class C in  which is subclass of B inside the list?



3 years ago
I modified it but it is still not working.

3 years ago
Hi All,

My question is on below code. I have created 3 dummy classes A, B and C. C is subclass of B and B is subclass of A. In the display method I am using an lower bounded generic type. The code is not compiling. Not sure how to fix it.



3 years ago
Hi All,

My question is on below code. I am able to run line 8 successfully since Object is parent of String class and there is impiicit type casting. But I am unable to run line 10 despite Object and String class having relationship and doing explicit typecasting. Why? Is there any example where I can successfully do explicit typecasting?

3 years ago
What is the difference between generic type T and wildcard?
3 years ago
Is Array a class in java? I am not talking about Arrays but I am talking about Array.
3 years ago
Hi All,

My question is on the below code. Why ifPresent is not working?

3 years ago
Hi All,

My question is why all the entries are not sorted?

Output:
1,one
2,two
3,three
4,four
---------
1,one
2,two
3,three
4,four

3 years ago
Why is the below code giving error?

Error:
prog.java:18: error: incompatible types: incompatible parameter types in lambda expression
      map.entrySet().stream().sorted((Map.Entry e)->e.comparingByKey()).forEach(e->System.out.println(e.getKey()+","+e.getValue()));

3 years ago