Question: I have a dictionary of dictionaries where the keys of the inner dictionary represents bins of a histogram and the values represent the frequency. I want to calculate the mean bin and the std of the bins.

```
dict = {'Group 1' : {1 : 100, 2:300, 4:100, 5:50},
       {'Group 2' : {1 : 50, 2: 300},
       {'Group 3' : {4 : 100, 5: 200},
        ...}

```

**Example**
For `Group 1` I want to get the mean and std identical to taking the mean and std of a list of 100 1's, 300 2's, 100 4's, 50 5's

```
l = []
l.extend([1 for j in range(0,100)])
l.extend([2 for j in range(0,300)])
l.extend([4 for j in range(0,100)])
l.extend([5 for j in range(0,50)])
np.mean(l) = 2.45
np.std(l) = 1.23

```

What would be the best way to iterate over each dictionary and transform it such that I get a dictionary of dictionaries representing the mean and std of the bins of the inner dictionaries?

```
transformed_dictionary = {'Group 1' : {'mean': 2.45 , 'std' : 1.23},
                          'Group 2' : {...},
                           ...}

```

What could be an efficient way of doing this?

Answer: 
Try to work with `ArrayList` collection.

1. First fill the ArrayList with values.
2. Then sort this collection, for example with util method `Collections.sort()`.
3. Finally, work on creating file. Write method for saving sorted collection value by value.
```
List<String> numbers = IntStream.range(0, 10).map(x -> random.nextInt(100) * (random.nextBoolean() ? -1 : 1)).boxed()
                .sorted(Collections.reverseOrder()).map(String::valueOf).collect(Collectors.toList());
Files.write(Paths.get("D://test.txt"), numbers);

```

Generates 10 random numbers, reverses, collects to list and then writes to a file.
However, this doesnot handle duplicate random numbers.