如何在Java 8中从流中收集cusom类对象?

In my problem I have Map<Flashcard, FlashcardStats> flashcards. Flashcard class contains two fields: String term and String definition. When I have to find specific flashcard from flashcards, how can I do I via Java 8 Streams? How can I collect Flashcard object or just definition field having only term?

例如:

    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

先感谢您!

评论
  • ad_est
    ad_est 回复

    假设一个处理结果

    Optional<Flashcard> flashcard = flashcards.keySet().stream().filter(o -> o.getTerm().equals(term)).map(Flashcard::getTerm).findFirst();