# Transform the map with multiple values ​​to the tree?

Given a randomly distributed set of keys, with each key mapped to a set of values, how would you transform it into multiple trees?

Example Data Set

• NB2 => {NC2 ND2}
• ND1 => {NG1 NH1}
• NA1 => {NB1}
• NB1 => {NC1 ND1 NE1}
• NA2 => {NB2}
• NC1 => {NF1}
• NE1 => {NI1 NJ1 NK1}

Resulting Tree for NA1

```NA1
`-- NB1
|-- NC1
|   `-- NF1
|-- ND1
|   |-- NG1
|   `-- NH1
`-- NE1
|-- NI1
|-- NJ1
`-- NK1
```

Resulting Tree for NA2

```NA2
`-- NB2
|-- NC2
`-- ND2
```

I'm not aware of any library methods that will do this transformation. Here's how I'd do it. It's pretty straightforward, IMO.

``````public class Tree {
public Tree(String key) {
// ...
}
// ...
}
}

public Set<Tree> transform(Map<String, List<String>> input) {
// Potential tree roots.  We start with all LHS keys as potential roots,
// and eliminate them when we see their keys on the RHS.
Set<String> roots = new HashSet<String>(input.keySet());

// This map associates keys with the tree nodes that we create for them
Map<String, Tree> map = new HashMap<String, Tree>();

for (Map.Entry<String, List<String>> entry : input.entrySet()) {
String key = entry.getKey();
List<String> childKeys = entry.getValue();
Tree tree = map.get(key);
if (tree == null) {
tree = new Tree(key);
map.put(key, tree);
}
for (String childKey : childKeys) {
roots.remove(childKey);
Tree child = map.get(childKey);
if (child == null) {
child = new Tree(childKey);
map.put(childKey, child);
}
}
}
Set<Tree> res = new HashSet<Tree>(roots.size());
for (String key : roots) {