есть такой метод
public List<DocType> mapDocType(List<ru.server.model.DocType> docTypes) {
List<DocType> result = new ArrayList<DocType>();
for(ru.server.model.DocType docType : docTypes) {
DocType temp = new DocType();
temp.setId(docType.getId());
temp.setDocident(docType.getDocident());
temp.setDocname(docType.getDocname());
result.add(temp);
}
return result;
}
надо переписать его, используя stream api, как это сделать правильно?
Чтобы было красиво, нужно сделать либо конструктор в DocType, либо аналогичный static метод вида:
public DocType(ru.server.model.DocType docType){
this.id = docType.getId();
this.docident = docType.getDocident();
this.docname = docType.getDocname();
}
либо
public static DocType of(ru.server.model.DocType docType){ //of/from/by, как хотите называйте
DocType temp = new DocType();
temp.setId(docType.getId());
temp.setDocident(docType.getDocident());
temp.setDocname(docType.getDocname());
return temp;
}
и после этого ваш метод преобразится в:
public List<DocType> mapDocType(List<ru.server.model.DocType> docTypes) {
return docTypes.stream()
.map(DocType::new)
.collect(Collectors.toList());
}
или, соответственно
public List<DocType> mapDocType(List<ru.server.model.DocType> docTypes) {
return docTypes.stream()
.map(DocType::of)
.collect(Collectors.toList());
}
Не знаю, какой тут смысл функционалки, но выглядит это примерно так:
public List<DocType> mapDocType(List<DocType> docTypes) {
return docTypes.stream().map((t) -> {
final DocType temp = new DocType();
temp.setId(docType.getId());
temp.setDocident(docType.getDocident());
temp.setDocname(docType.getDocname());
return temp;
}).collect(Collectors.toList());
}
Но лучше добавьте конструктор в класс DocType и передавайте в него id, docident, docname. Тогда все будет проще:
public List<DocType> mapDocType(List<DocType> docTypes) {
return docTypes.stream()
.map(t -> new DocType(docType.getId(), docType.getDocident(), docType.getDocname()))
.collect(Collectors.toList());
}
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости