Is there helper method to convert table to tree

Hi,

I want to convert Table structure to Tree, for example,

 key  data
 1      x1
 1      x2
 2      x3

I want to get

class{
int key;
List<int> data;
}

Do you have any good idea. i must use double loop to create the objects every time. It’s so cool that SS has utils to handle this.