PriorityQueue is not sorted. It is guaranteed that only the first element is "sorted" (PriorityQueue is specified only to return lowest element every time head is requested). The whole queue could be sorted, but this is not guaranteed.
So PriorityQueue iterator returns elements in any order, not in sorted order. Assuming sorted order is a common bug.
Thursday, December 15, 2016
Monday, November 21, 2016
++ operator on variable used multiple times in single line
Write the output of this code:
long l = 10;
System.out.println( "l " + l++ + "; " + l );
Result:
l 10; 11
long l = 10;
System.out.println( "l " + l++ + "; " + l );
Result:
l 10; 11
Friday, November 18, 2016
Tuesday, May 10, 2016
Java: Variables in interfaces
Concept of variables in interfaces is allowed in Java, but the concept is different. Variables defined in interfaces are always static and final.
Check this for more details.
Check this for more details.
Wednesday, May 4, 2016
Java syntax hack
These are equivalent code segments:
Hacky syntax:
Map map = new HashMap() {{
put("key", "value");
}};
Normal syntax:
Map map = new HashMap();
map.put("key", "value");
Decryption of encrypted syntax
First set of braces is the anonymous inner class (subclassing HashMap). The second set of braces is an instance initializer (rather than a static one) which then sets the values on your HashMap.Usages
This hacky syntax could be used gracefully when initializing a class member. This way you will avoid adding code to constructor(s) or in static initializer of the class.Anonymous class that “extends” other class or “implements” some interface in Java?
Anonymous classes in Java are defined in this way:
Syntax for implementing some interface:
Syntax for extending other class (is the same):
Syntax for implementing some interface:
Runnable r = new Runnable() {
public void run() { ... }
};
Syntax for extending other class (is the same):
SomeClass x = new SomeClass() {
...
};
Wednesday, March 16, 2016
BigDecimal
Tutorials:
1. http://www.javaworld.com/article/2075315/core-java/make-cents-with-bigdecimal.html
2. https://topic.alibabacloud.com/a/how-to-use-java-bigdecimala-tutorial__java_1_27_20230422.html
Discussions:
Double vs BigDecimal: http://stackoverflow.com/questions/3413448/double-vs-bigdecimal
Most important:
1. BigDecimals are immutable.
1. http://www.javaworld.com/article/2075315/core-java/make-cents-with-bigdecimal.html
2. https://topic.alibabacloud.com/a/how-to-use-java-bigdecimala-tutorial__java_1_27_20230422.html
Discussions:
Double vs BigDecimal: http://stackoverflow.com/questions/3413448/double-vs-bigdecimal
Most important:
1. BigDecimals are immutable.
Subscribe to:
Posts (Atom)