Last active
November 18, 2018 23:46
-
-
Save codinko/97114d32e7d9f2647ea27d3f99e409f2 to your computer and use it in GitHub Desktop.
delete a note using java stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean deleteNote(String userId, int noteId) { | |
NoteUser dbUser = this.noteRepository.findById(userId).orElse(null); | |
Note dbNote = null; | |
if(dbUser != null) { | |
dbNote = this.findNote(noteId, dbUser.getNotes()); | |
if(dbNote != null) { | |
dbUser.getNotes().remove(dbNote); | |
dbUser = this.noteRepository.save(dbUser); | |
} | |
} | |
return dbNote != null; | |
} | |
private Note findNote(int noteId, List<Note> notes) { | |
return notes.stream().filter(note -> note.getNoteId() == noteId) | |
.findAny() | |
.orElse(null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment