希望有人會已經解決了這個,嵌套集合塊迭代和範圍在Lambda表達式
對象結構我有類似
主要對象一個對象結構是學生,學生得到了一些信件
public class LetterRange {
private Date letterStartDate;
private Date letterEndDate;
public Date getLetterStartDate() {
return letterStartDate;
}
public void setLetterStartDate(Date letterStartDate) {
this.letterStartDate = letterStartDate;
}
public Date getLetterEndDate() {
return letterEndDate;
}
public void setLetterEndDate(Date letterEndDate) {
this.letterEndDate = letterEndDate;
}
}
public class Letters {
private String letterName;
private Set<LetterRange> letterRangeSet;
public String getLetterName() {
return letterName;
}
public void setLetterName(String letterName) {
this.letterName = letterName;
}
public Set<LetterRange> getLetterRangeSet() {
return letterRangeSet;
}
public void setLetterRangeSet(Set<LetterRange> letterRangeSet) {
this.letterRangeSet = letterRangeSet;
}
}
public class Student {
private String name;
Set<Letters> lettersSet;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Letters> getLettersSet() {
return lettersSet;
}
public void setLettersSet(Set<Letters> lettersSet) {
this.lettersSet = lettersSet;
}
}
public class StudentRunner {
public static void main(String args[]){
List<Student> studentList= new ArrayList<>();
Student one= new Student();
one.setName("John");
Letters johnLetter1= new Letters();
johnLetter1.setLetterName("Kudos1");
LetterRange johnLetter1Range= new LetterRange();
johnLetter1Range.setLetterStartDate(new Date());
johnLetter1Range.setLetterEndDate(new Date());
LetterRange johnLetter2Range= new LetterRange();
johnLetter1Range.setLetterStartDate(DateTime.now().plus(10).toDate());
johnLetter1Range.setLetterEndDate(DateTime.now().plus(10).toDate());
johnLetter1.setLetterRangeSet(new HashSet<LetterRange>(){{add(johnLetter1Range); add(johnLetter2Range);}});
one.setLettersSet(new HashSet<Letters>(){{add(johnLetter1);}});
studentList.add(one);
studentList.forEach(student -> student.getLettersSet().stream().flatMap(letters -> letters.getLetterRangeSet()));
}
}
我堅持在哪裏試圖讓lambda返回給我給所有學生的所有letterRanges。我知道我們可以迭代舊的方式,但我試圖看看是否有一個lambda可以幫助我。 TIA。
更改僞一點,謝謝 –
讓我複製粘貼代碼,。少數。更新它。 –
你真的複製粘貼這段代碼嗎?代碼中有很多明顯的錯誤! (例如,「類」而不是「類」) –