there is such a method

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; } 

need to rewrite it using stream api , how to do it right?

  • one
    I think your example is more readable than when you rewrite it to stream api - Senior Pomidor 1:54 pm
  • maybe it is, but the task is to rewrite it to stream api - Ilya Dyachkov
  • one
    And what is the fundamental difference created by temp from docType , which is in a loop? If you end up with a new list with new objects, then look at stackoverflow.com/questions/715650/… . There are examples using the Stream API. I also recommend baeldung.com/java-copy-list-to-another . There are quite a few examples. - gooamoko 2:29 pm
  • I edited the question, indicated that the docType is in a loop from another package - Ilya Dyachkov

2 answers 2

To be beautiful, you need to do either the constructor in the DocType , or a similar static method of the form:

 public DocType(ru.server.model.DocType docType){ this.id = docType.getId(); this.docident = docType.getDocident(); this.docname = docType.getDocname(); } 

or

 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; } 

and after that your method will be transformed into:

 public List<DocType> mapDocType(List<ru.server.model.DocType> docTypes) { return docTypes.stream() .map(DocType::new) .collect(Collectors.toList()); } 

or, respectively

 public List<DocType> mapDocType(List<ru.server.model.DocType> docTypes) { return docTypes.stream() .map(DocType::of) .collect(Collectors.toList()); } 
  • I can not edit the class DocType - Ilya Dyachkov
  • Well, do it not in DocType , but create a separate class, such as DocTypeFactory or DocTypeConverter or something else, call it, and write down a static method. Yes, there will be an extra class in order to simplify the method, but often this is done. A little more code is written to make it easier to work with it in the future. - iksuy

I do not know what the meaning of the functionary is here, but it looks like this:

 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()); } 

But it is better to add a constructor to the DocType class and pass id , docident , docname . Then everything will be easier:

 public List<DocType> mapDocType(List<DocType> docTypes) { return docTypes.stream() .map(t -> new DocType(docType.getId(), docType.getDocident(), docType.getDocname())) .collect(Collectors.toList()); } 
  • The meaning of the functionary is that the Doctype in for and Doctype temp are different classes, I did not indicate this in the code - Ilya Dyachkov
  • It does not change the answer, nor does it fill it with meaning! Create an instance of the class you need in the code I wrote and you will be happy. - Dmitry