Question: I'm doing a project for college and have hit a wall with how to search through my hashmap and add the results to a List.

So far i have a Recommendation class with these variables as well as getters,setters constructor and hashcode and equals : 

```
private String title, queryName,Director,summary,myComments,category;
private double Rating;
private int myRating;
private ArrayList<String> cast = new ArrayList<>();

```

So what i want to do is search through this map of Recommendations for an actors name and add the results to a List. This is what i've done so far : 
 private Map recs = new HashMap<>();

```
public void byActor(String actor)
{

    if(recs.containsKey(actor))
    {

        System.out.println(" The Actor "+actor + " has been in these films ");

    }

```

If the actor isn't in the hashmap i have to get recommendations off a website API,which should return it as a list or recommendations and add it to the hashmap ( which will be a local store ) 

How do I add the results of the search to a list and add a list to a hashmap?

Answer: 
Assuming you want to have a list of `Recommendation`s for each actor in your map, you need a `Map<String, List<Recommendation>>`. This is called a *multimap*.

```
Map<String, List<Recommendation>> recommendationsByActor = new HashMap<>();

```

If you are using Java 8, you can use the [`computeIfAbsent` method](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function-) to add a recommendation for an actor, as follows:

```
recommendationsByActor
    .computeIfAbsent(actorName, k -> new ArrayList<>())
    .add(recommendation);

```

This will get the list of recommendations for an actor, if there were already one or more recommendations for that actor in the map. If there were no recommendations for that actor yet, `computeIfAbsent` will create an empty `ArrayList` for that actor and will put it into the map. *In both cases, the given recommendation will be added to the list of recommendations for that actor*.

If you just want to get the list of recommendations for a given actor, simply do:

```
List<Recommendation> recommendations = recommendationsByActor.get(actorName);

```

This will return the list of the recommendations for a given actor, or `null` if there are no recommendations for him/her.

**EDIT:**

If instead of creating an empty `ArrayList` for an actor who has no recommendations in the map, you need to get recommendations off a website API, you can change the call to `computeIfAbsent` as follows:

```
List<Recommendation> recommendations = recommendationsByActor
    .computeIfAbsent(actorName, this::getRecommendationsForActorFromWebAPI);

```

Where `getRecommendationsForActorFromWebAPI` would be an instance method in the same class, with the following signature:

```
private List<Recommendation> getRecommendationsForActorFromWebAPI(String actorName) {

    // TODO do the dirty work: call API and return recs list for actor
}

```

In this case, `computeIfAbsent` will return the list of recommendations for the given actor, either locally from the map (if the list is already present), or it will get the list of recommendations from the API call and add it to the map for that actor, before returning it.
To add a data on a list

```
myList.add(myData);

```

to add a data from a list to a map :

```
myMap.put(myKey, myValue);

```

if you want to add every data from the list : 

```
for(dataType data : myList){ // this is a foreach
  myMap.put(myKey, myValue);
}

```

Cheers