As part of a project where I’m building a simulator I wanted a pathfinding algorithm and looked around for an A*-library in Java. I found a few that I didn’t like the look of and decided to just make my own library. Luckily the algorithm is simple enough to basically copy-paste from the pseudocode found on Wikipedia, and it was during the rewrite I decided to really look into generics for the first time.
Below is an example of generics (note that the methods returns “T“, and not an int or string. Basically, if we send a specific object that implements AStarNode to these methods, we get an object of the same type back.
public interface AStarNode { public <T extends AStarNode> Collection<T> getNeighbours(); public <T extends AStarNode> double getDistance(T node); }