Java doesn’t provide tuples. Here are some ideas what you can do about it. Continue reading “Tuples in Java”
So in Java there are many kinds of methods. I probably won’t even list half of them but I try to explain how different methods can be.Continue reading “Types of Methods in Java”
Functional Programming made easy!
Continue reading “Functional Programming in Java 8”
New is used to create new objects. But it’s used carelessly.Continue reading “Misconceptions about the new Operator”
In Java you can only reference final fields inside a closure. Here’s my solution*.Continue reading “Recursive Closures in Java”
Generics have one thing in common with regular expressions: Students think they are an easy to use tool but they just end up with code they do not understand and can’t maintain. Regular expressions can often be replaced by well written and documented code. But you can’t just replace generics. A student using collections with generic type parameters but not fully understanding the concept is still better than one using “raw” collections. And it’s often ok to use something just to get familiar with the idea before learning the theory. The downside is that many misconceptions emerge from this approach of teaching. In this post I try to explain some of the common misconceptions on generics in Java.Continue reading “Generics”
Here’s an implementation of a multiset in Java 8. It uses a
Map<T, Integer> to count how many times an object was inserted (multiplicity). It is not thread-safe, but there’s
synchronizedMultiset(...) and it always knows the current size in
O(1). With the method
asMap() you can get a view of the multiset as a
Map, that will allow modification of the underlying multiset.
The code is on GitHub: github.com/claudemartin/multiset
This started as a simple implementation and is not rather complex. The iterator allows to remove elements during iteration. I tried to write my own
Spliterator, but it wasn’t faster than the default implementaion. Many methods are optimized and I added some set operations for multisets (union, intersect, minus). With
merge(...) you can merge two multisets with any operation.
Most tutorials use some recursive Task to calculate a Fibonacci number or something similar. This leads to the false impression that Fork/Join can only handle recursive Tasks (e.g. Divide-And-Conquer). Here’s a very simple example of a non-recursive Task.
Here’s an example to show how to implement
compareTo in a decorator.
Continue reading “Decorator implementing Comparable”
Abstraction is very important in Java. And there’s the keyword abstract. But the use of abstract is not abstraction. It’s a common misconception that the abstract keyword is used for abstraction.
Continue reading “Abstraction vs abstract”