I've got a movie database (postgreSQL). Among tables consists of stars and movie game titles. A job which i've to resolve in java is the following: two stars (A and B) are connected together once they within the same movie. Further two stars, A and B, will also be connected when there's another actor C, who plays with each of them in various movies (A and B don't play together!) and so forth... I really hope you get the drift :) Now I must discover the least connection (= path) between two stars.

How to the implementation: fetching the information in the DB (prepared claims) and saving what they are called (as strings) inside a linked list is working. And easy link between stars just like a -> B (= both participate in the same movie). I am striking the wall attempting to include more difficult connections (just like a -> B -> C).

I'm storing the actor names inside a HashMap such as this:

Map<String, List<String>> actorHashMap = new HashMap<String, List<String>>();

Then when I load the very first actor (The Actor-brad Pitt) I've his title as key, along with other stars having fun with him in a listing recommended through the key. Checking, whether another actor performed with him is simple:

List<String> connectedActors = actorHashMap.get(sourceActor);
if(connectedActors.contains(actor)) {
         found = true; }

But... exactly what do I actually do when the actor I am searching for isn't within the HashMap (ie. when I must go one level much deeper to locate him)? I suppose I would need to select the first actors' title make up the connectedActors list, place it as being key in to the HashMap, and fetch all stars he performed with him to place these to. Then search within this list.
But that is precisely the part that we can't determine. I already attempted to keep what they are called in graph nods and taking advantage of bfs to find them, but same issue here, just don't understand how to go "one level lower" without creating an infinite loop... Does anybody know the way i can solve this? I'm just beginning with java in addition to programing generally therefore it is most likely simple, however i cannot view it :/

First I'd make use of a Set to keep the stars someone performed with:

Map<String, Set<String>> actorHashMap = ...

rather than a List to prevent duplicate names.

To be able to find the number of levels of separation between 2 stars I'd begin with one actor and generate all stars which are separated by 1, 2, 3... levels. I'd fix a maximum search depth, however that is probably not necessary if the amount of stars isn't too big.

String actor = "Johnny Depp";
String targetActor = "John Travolta";
Map<String, Integer> connectedActorsAndDepth = new HashMap<String, Integer>();
Integer depth = 1;
Set<String> actorsAddedAtCurrentDepth = actorHashMap.get(actor);
for (String otherActor : actorsAddedAtPrecedingDepth) {
    if (otherActor.equals(targetActor)) return depth;
    connectedActorsAndDepth.put(otherActor, depth);
}
Set<String> actorsAddedAtPrecedingDepth = actorAddedAtCurrentDepth;
Integer maxDepth = 10;
while (++depth < maxDepth) {
    actorsAddedAtCurrentDepth = new HashSet<String>():
    for (String otherActor : actorsAddedAtPrecedingDepth) {
       if (otherActor.equals(targetActor)) return depth;
       if (!connectedActorsAndDepth.contains(otherActor)) {
           actorsAddedAtCurrentDepth.add(otherActor);
           connectedActorsAndDepth.put(otherActor, depth);
       }
    }
    actorsAddedAtPrecedingDepth = actorsAddedAtCurrentDepth;
}

I don't claim it's the most effective formula. There may be also bugs within the code above.