Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java stream API parent child relation

I have a JSON file that represent dependence parent -> child (subject -> phrase). I initially load that JSON into one list of MyClass

public class MyClass {

    String subject_name;
    String subject_color;
    Integer subject_weight;
    String phrase_name;
    Integer phrase_weight;      
}

So finally i'll have Collection<MyClass>;

[
  {
    "subject_name": "ADMINISTRATION - Bureaucracy and Org Issues",
    "subject_color": "black",
    "subject_weight": 10,
    "phrase_name": "admin burden"
    "phrase_weight": 1
  },
  {
    "subject_name": "ADMINISTRATION - Bureaucracy and Org Issues",
    "subject_color": "red",
    "subject_weight": 10,
    "phrase_name": "user burden",
    "phrase_weight": 2
  },
  {
    "subject_name": "ADMINISTRATION - Bureaucracy and Org Issues",
    "subject_color": "blue",
    "subject_weight": 10,
    "phrase_name": "client burden",
    "phrase_weight": 3
  },
  {
    "subject_name": "ADMINISTRATION - Data Tools and Systems",
    "subject_color": "white",
    "subject_weight": 10,
    "phrase_name": "Computer level",
    "phrase_weight": 4
  },
  {
    "subject_name": "ADMINISTRATION - Data Tools and Systems",
    "subject_color": "black",
    "subject_weight": 10,
    "phrase_name": "Computer system",
    "phrase_weight": 10
  },
  {
    "subject_name": "ADMINISTRATION - Data Tools and Systems",
    "subject_color": "red",
    "subject_weight": 10,
    "phrase_name": "Computer tools",
    "phrase_weight": 2
  }    
]  

I have another classese:

public Phrase {
    String phrase_name;
    Integer phrase_weight;  
}

public Subject {
    String subject_name;
    String subject_color;
    Integer subject_weight;
    Set<Phrase> phrases;
}

How to use Java 8 stream to finally optain Subject object from Collection collection. I'm able to optain just Subject list but not sure how to handle with Phrase subset. Here is the code that I have:

@Test
public void testUploadJSON() throws FileNotFoundException {

    String json = "C:\\ttt\\file.json";

    Gson gson = new Gson();
    BufferedReader br = new BufferedReader(new FileReader(json));
    Type type = new TypeToken<Collection<MyClass>>() {
    }.getType();
    Collection<MyClass> jsonData = gson.fromJson(br, type);

    Set<Subject> subjects = jsonData.stream().map(myClass -> {
        Subject subject = new Subject();
        subject.setName(myClass.getSubjectName());
        subject.setColor(myClass.getSubjectColor());
        subject.setWeight(myClass.getSubjectWeight());
        return subject;
    }).collect(Collectors.toSet());

    System.out.println(subjects.size());

    assertFalse(jsonData.isEmpty());
}

UPDATE I just update the input JSON file with more attributes for Subject and Phrase.

like image 342
Dumitru Gutu Avatar asked Feb 19 '26 17:02

Dumitru Gutu


1 Answers

You can do something like this:

 Map<String, Set<String>> collect = 
 jsonData.stream().collect(Collectors.groupingBy(MyClass::getSubject_name, Collectors.mapping(MyClass::getPhrase_name, Collectors.toSet())));

You have to add getters and setters to your MyClass in order to be able to use method reference

like image 50
Schidu Luca Avatar answered Feb 22 '26 06:02

Schidu Luca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!