elimu-ai / webapp

🖥 Web application for hosting Android applications and educational content
https://hin.elimu.ai
MIT License
30 stars 55 forks source link

Use Java streams to simplify the creation of JSON objects #1743

Closed jo-elimu closed 1 month ago

jo-elimu commented 1 month ago
          <!-- [monologue]

[task 1/1] The user has requested an example of the JSON object creation using Java streams. [observations] The original code iterates over LetterSound objects and manually constructs JSON objects. The suggested change uses Java streams to simplify the creation of these JSON objects. [analysis] To provide a clear example, we need to show the before and after code snippets, demonstrating the use of Java streams for creating JSON objects. [actions] Generate a detailed response showing the original and optimized code snippets for JSON object creation. -->

@jo-elimu Sure, here's an example demonstrating the use of Java streams to create JSON objects for LetterSound entities.

Original Code

The original code manually constructs JSON objects in a loop:

JSONArray letterSoundsJsonArray = new JSONArray();
int index = 0;
for (LetterSound letterSound : word.getLetterSounds()) {
    JSONObject letterSoundJsonObject = new JSONObject();
    letterSoundJsonObject.put("id", letterSound.getId());
    String[] lettersArray = new String[letterSound.getLetters().size()];
    for (int i = 0; i < lettersArray.length; i++) {
        lettersArray[i] = letterSound.getLetters().get(i).getText();
    }
    letterSoundJsonObject.put("letters", lettersArray);
    String[] soundsArray = new String[letterSound.getSounds().size()];
    for (int i = 0; i < soundsArray.length; i++) {
        soundsArray[i] = letterSound.getSounds().get(i).getValueIpa();
    }
    letterSoundJsonObject.put("sounds", soundsArray);
    letterSoundJsonObject.put("usageCount", letterSound.getUsageCount());
    letterSoundsJsonArray.put(index, letterSoundJsonObject);
    index++;
}

Optimized Code

The optimized code uses Java streams to simplify the creation of JSON objects:

JSONArray letterSoundsJsonArray = new JSONArray();
int index = 0;
for (LetterSound letterSound : word.getLetterSounds()) {
    JSONObject letterSoundJsonObject = new JSONObject();
    letterSoundJsonObject.put("id", letterSound.getId());
    letterSoundJsonObject.put("letters", letterSound.getLetters().stream().map(Letter::getText).toArray(String[]::new));
    letterSoundJsonObject.put("sounds", letterSound.getSounds().stream().map(Sound::getValueIpa).toArray(String[]::new));
    letterSoundJsonObject.put("usageCount", letterSound.getUsageCount());
    letterSoundsJsonArray.put(index, letterSoundJsonObject);
    index++;
}

In this optimized version, the letters and sounds arrays are created using Java streams, which makes the code more concise and easier to read.

Let me know if you need any further assistance!


Learnings used ``` Learnt from: venkatesh2k3 PR: elimu-ai/webapp#1682 File: src/main/java/ai/elimu/dao/LetterSoundContributionEventDao.java:13-13 Timestamp: 2024-07-06T17:37:44.413Z Learning: In the `LetterSoundContributionEventDao.java` file, the method signatures should use `LetterSoundContributionEvent` instead of `LetterSoundCorrespondenceContributionEvent` to maintain consistency with the renaming of entities. ```

_Originally posted by @coderabbitai[bot] in https://github.com/elimu-ai/webapp/pull/1725#discussion_r1677009542_

SnehaHS65 commented 1 month ago

I would like to take up this issue after my PR( https://github.com/elimu-ai/webapp/pull/1725#top) gets merged since it is directly related.