0

Im new in java

Lets say I have

class OnlyNotes {
        String notes;
    }

and

List<OnlyNotes>;

how to convert it to:

List<String>

That contains list of notes

1
  • 4
    What have you tried so far? Commented Jul 12, 2021 at 7:39

2 Answers 2

2

You can write like this.

List<OnlyNotes> onlyNotes = new ArrayList<>();
List<String> newStrings = onlyNotes.stream.map(e -> e.notes).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

0

Assumning your class looks like

class OnlyNotes {

  String notes;

  private OnlyNotes(String pNotes) {
     super();
     notes = pNotes;
  }

  public String getNotes() {
     return notes;
  }
}

you could do that by using for-loop or by using stream

List<OnlyNotes> onlyNotesList =
            Arrays.asList(new OnlyNotes("first"), new OnlyNotes("second"), new OnlyNotes("third"));

List<String> onlyNotesAsStringList = new ArrayList<>();
for (OnlyNotes onlyNotes : onlyNotesList) {
         onlyNotesAsStringList.add(onlyNotes.getNotes());
}

List<String> onlyNotesAsStringByStreamList = onlyNotesList.stream().map(OnlyNotes::getNotes).collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.