Testing connectivity and finding connected components are classic tasks when working with graphs. Recall that depending on whether the graph is directed or not, we have two different notions of connectivity, i.e. strong and weak connectivity.
jgrapht.algorithms.connectivity