Best way to use a Java 8 stream with class and subclass

0

Dear community, I have a problem in how to use streams, I have 3 classes:

public Class A{
 String string1A;
 String string2A;
 List<B> listB;
 .
 .
 .
 }
 public Class B{
 String string1B;
 String string2B;
 .
 .
 .
 }
 public Class C{
 String string1A;
 String string2A;
 String string1B;
 String string2B;
 .
 .
 .
 }

Then I have a method that returns a List of C with all the data coming from a database, and I need to create a List A that has all the data grouped, as the grouping value is the String1A, what I know. it happened was this:

List<A> listaA = new ArrayList<A>();

    Set<String> listString1A = listaC.stream().map(x->x.getString1A()).distinct().collect(Collectors.toSet());

for(String stringFilter1A: listString1A){
  A a = listaC.stream().filter(x->getString1A().equals(stringFilter1A)).map(x-> new A(x.getString1A(),x.getString2A)).findFirst().get();
 List<B> listaB = listaC.stream().filter(x->getString1A().equals(stringFilter1A)).map(x-> new B(...)).collect(Collectors.toList());
 a.setListB(listaB);
 listaA.add(a);
}

Is there a way to do this query using only streams or trying to delete the for?

    
asked by chavalife17 15.10.2018 в 18:54
source

0 answers