Question: I'm pretty new.

I wrote this python script to make an API call from blockr.io to check the balance of multiple bitcoin addresses.

The contents of btcaddy.txt are bitcoin addresses seperated by commas. For this example, let it parse [this](http://btc.blockr.io/api/v1/address/info/1FQCJjV3JwoQVgRYVRmDUFyZD9rt946Yp9,1BitmixerEiyyp3eTLaCpgBbhYERs48qza,1CyDUaBBhZAwGZEnL266Kb4tRQ1Br9Rj8c).

```
import urllib2
import json

btcaddy = open("btcaddy.txt","r")

urlRequest = urllib2.Request("http://btc.blockr.io/api/v1/address/info/" + btcaddy.read())
data = urllib2.urlopen(urlRequest).read()
json_data = json.loads(data)

balance = float(json_data['data''address'])
print balance

raw_input()

```

However, it gives me an error. What am I doing wrong? For now, how do I get it to print the balance of the addresses?

Answer: 
You can use `findFirst()` to build a list that contains, for each town, the first route that has that town as the destination, and then call `toMap()` on it. The default values for missing cities can be handled separately.

```
Collection<Town> towns = getAllTowns(routes);
Map<Town, Integer> distances = towns.stream()
    .filter(town -> !originTown.equals(town))
    .map(town -> routes.stream()
        .filter(route -> route.hasOrigin(originTown) && route.hasDestine(town))
        .findFirst())
    .filter(Optional::isPresent)
    .collect(toMap(route -> route.get().getDestine(), route -> route.get().getDistance()));
towns.stream()
    .filter(town -> !distances.containsKey(town))
    .forEach(town -> distances.put(town, maxDistance));

```

(Note that `town` is no longer available in `collect()`, but you can take advantage of the fact that each route got added only if its destination town was `town`.)

Also note that `toMap()` doesn't accept duplicate keys. If there can be multiple routes to any town (which I assume there might be), you should use `groupingBy()` instead.
I think you have two options to solve this. Either you create your resulting `Map` beforehand and use nested `foreach`s:

```
Map<Town, Integer> distances = new HashMap<>();
    getAllTowns(routes).stream().filter(town -> !originTown.equals(town))
            .forEach(town -> routes.stream().forEach(route -> distances.put(town,
                    route.hasOrigin(originTown) && route.hasDestine(town) ? route.getDistance() : maxDistance)));

```

The other option is to `collect` your stream to a `Map` by creating an intermediate `Object` which is essentially a Pair of `Town` and `Integer`:

```
Map<Town, Integer> distances = getAllTowns(routes).stream().filter(town -> !originTown.equals(town))
            .flatMap(town -> routes.stream()
                    .map(route -> new AbstractMap.SimpleEntry<Town, Integer>(town,
                            route.hasOrigin(originTown) && route.hasDestine(town) ? route.getDistance()
                                    : maxDistance)))
            .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue())); 

```