Dynamische Gruppierung nach bestimmten Attributen mit Collection.stream

Ich versuche, eine Liste von Objekten mithilfe von Java 8 Collection-Stream nach mehreren Attributen zu gruppieren.

Das funktioniert ganz gut:

public class MyClass
{
   public String title;
   public String type;
   public String module;
   public MyClass(String title, String type, String module)
   {
      this.type = type;
      this.title = title;
      this.module= module;
   }
}

List<MyClass> data = new ArrayList();
data.add(new MyClass("1","A","B"));
data.add(new MyClass("2","A","B"));
data.add(new MyClass("3","A","C"));
data.add(new MyClass("4","B","A"));

Object result = data.stream().collect(Collectors.groupingBy((MyClass m) 
-> m.type, Collectors.groupingBy((MyClass m) -> m.module)));

Aber ich würde es gerne etwas dynamischer machen. Ich möchte nur ein String-Array (oder eine Liste) angeben, das für GroupBy verwendet werden soll.

Etwas wie

Object groupListBy(List data, String[] groupByFieldNames)
{
    //magic code
}

und ich möchte anrufen:

groupListBy(data, new String[]{"type","module"});

Wie kann ich die groupBy-Methode dynamischer gestalten, wie in meinem Beispiel?

Antworten auf die Frage(4)

Ihre Antwort auf die Frage