The given implementation for hashCode
is somewhat a mystery. Some misconceptions about it confuse students. I try to explain it. Continue reading “Misconceptions about Object.hashCode()”
Category: Java
About Java – the technology, not the island.
Generics
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”Multiset in Java
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.
Project: String Wrappers
Yet another side project. But this isn’t usable as it’s not finished and not really tested. The idea is to provide wrapper classes for character sequences so that strings aren’t just copied and some memory can be saved.
The project is hosted here: https://github.com/claudemartin/stringwrappers/
Fork/Join with non-recursive Task
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.
Decorator implementing Comparable
Here’s an example to show how to implement compareTo
in a decorator.
Continue reading “Decorator implementing Comparable”
Abstraction vs abstract
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”
Does Java support Multiple Inheritance?
This is probably often asked at interviews. But the question is incomplete.
Java 8 StreamZipper
The code (it’s a single class) is on pastebin:
https://pastebin.com/R5Sx07HC
This code is from an early build of Java 8 Beta. This was removed later and no zip function exists in the final Java 8 release. Here’s the source I used:
changeset 8789:569088178418.
Misnomer: Cloneable
In my category for Java Misnomers I really should write about this silly interface.
Continue reading “Misnomer: Cloneable”