I have a model class:
public class Person{
private String personnelId;
private String lastName;
private String firstName;
}
Now I have service class
public class A{
@Autowired
private OfficerService officer;
public List<Person> getAccounts(Param param){
List<Person> personList = personDao.getAccounts(param);
List<String> stringList = personList.stream().map(Person::getPersonnelId).collect(Collectors.toList());
List<String> list = Officer.getVisiblePerson(stringList);
List<Person> visiblePersonList= new ArrayList<>();
return visiblePersonList;
}
Now when I convert List<Person> into List<String> which gets the personnelId from Model class. After the method call I get back personnelId. I need to convert back into List<Person> which will contain all the data with firstName and lastName in Person class attached to that Id. How can I do that?
EDIT:
Map<String, Person> personMap = personList.stream()
.collect(Collectors.toMap(Person::getPersonnelId, person -> person));
List<Person> visiblePersonList = list.stream()
.map(id -> personMap.get(id))
.collect(Collectors.toList());
I get an error on collect of map:
The method collect(Collector<? super String,A,R>) in the type Stream<String> is not applicable for the arguments (Collector<Person,capture#2-of ?,Map<String,Object>>)
What am i doing wrong?
List<String>really necessary?Officer.getVisiblePerson(...)do?