Reputation: 738
I have a Map<Flashcard, FlashcardStats> flashcards
. The Flashcard class contains two fields: String term and String definition. When I have to find specific flashcard from flashcards, how can I do it via Java 8 Streams? How can I collect Flashcard object or just definition field having only term?
For example:
private static String getWrongAnswerMessage(Map<Flashcard, FlashcardStats> flashcards, String term, String answer) {
Flashcard flashcard = flashcards.keySet().stream().filter(o -> o.getTerm().equals(term)).collect() //what now
Upvotes: 3
Views: 1685
Reputation: 2011
Assuming on one maching result
Optional<String> term = flashcards.keySet()
.stream()
.filter(o->o.getTerm().equals(term))
.map(Flashcard::getTerm)
.findFirst();
Upvotes: 5