0

I have a Student object constructed with 3 parameters; id, startdate, enddate. I have created a Map using Student class. I want to extract the students who have the same startDate and endDate s with the given date parameters. As I understand I can access all the 3 parameters with requestparams.values() but since I want to compare dates separately I need to access each one of them. Is there a method or a way to do this?

Some parts of my code

public class StudentService {

    private Map<String,Student> requestparams = DatabaseClass.getStudents();

    public StudentService()
    {
        requestparams.put("123", new Student("123", "2017" , "2018"));
        requestparams.put("1234", new Student("1234", "2016" , "2017"));
        requestparams.put("8563", new Student("8563", "2015" , "2016"));
    }


  public List<Student> getFilteredStudent(String id, String startDate, String endDate)
  {
    List<Student> filteredStudents = new ArrayList<>();

    for(String key : requestparams.keySet()){
        if(key.equals(id) && /*requestparams.values().startDate == startDate something like this*/ ){
        filteredStudents.add(requestparams.get(key));
        }
    }
    return filteredStudents;
}
csel
  • 187
  • 2
  • 5
  • 19
  • You can loop over `requestParams.values()` – Nicholas K Aug 28 '18 at 13:51
  • And hint: you already have a matching KEY. And in the next line you figured you can do `requestparams.get(key))`. You could also use that *before* to fetch the fields of that entry. Or you iterate `entrySet()` directly. Many ways how to solve this. – GhostCat Aug 28 '18 at 13:55
  • You can iterate over values and filter with your condition as : `List filteredStudents = requestparams.values().stream() .filter(s -> s.getId().equals("id")) .filter(s -> s.getStartDate().equals(s.getEndDate())).collect(Collectors.toList());` – Emre Savcı Aug 28 '18 at 13:59

0 Answers0