1

I have one list where it may has maximum 2 values OR minimum 0 values. Now I need to set the first item value and second item value to my pojo class.

List<student> studentList  = studentRepository.findById(idval);


studentList.forEach(student -> {

            st.setStartTime(student.getStartTime()); // fetch from 1st item
            st.setEndTime(student.getEndTime()); // fetch from 1st item
            st.setNextStartTime(student.getStartTime()); // fetch from 2nd item
});

Here I need to set values to my studentList pojo. In that pojo I need to set 3 attributes where first two attribute values are from the 1st item of the list and the third attribute value is from the 2nd item of the list.

Can anyone suggest some good way to do the same.

EDIT: I have a list (studentList) which has two student class object items. (Lets assume the list has studentA and studentB information). Now I need to iterate the studentList and set the values to one pojo (st). In the st pojo I have 3 attributes are there. here I need to set the first two attribute values with studentA information and the third attribute value with studentB information.

4
  • 2
    Can you edit your question to add some details? It's unclear ... Commented Jan 6, 2016 at 12:37
  • updated my question with Edit Commented Jan 6, 2016 at 12:45
  • 1
    Why are you overwriting st with the second student's data? Why not just get the second student from the list? Or are you trying to cater for the 1-student case? Commented Jan 6, 2016 at 12:47
  • 1
    Why do you say you "need to iterate" when in fact you just want to get specifically the 1st and 2nd elements? Commented Jan 6, 2016 at 13:10

4 Answers 4

3

It seems to me that this isn't a suitable candidate for iteration - you're not doing the same thing with each element in the collection and, further, you've got a different behaviour for the 'empty collection' case than for the 'not-empty collection' case.

I'd go with this...

List<student> studentList  = studentRepository.findById(idval);
if (studentList.size() == 2) {
    st.setStartTime(studentList.get(0).getStartTime()); // fetch from 1st item
    st.setEndTime(student.get(0).getEndTime()); // fetch from 1st item
    st.setNextStartTime(student.get(1).getStartTime()); // fetch from 2nd item
}

It's more expressive than a loop or a collection transformation, because you're not really trying to do either.

Caveat:- you probably want to handle size() == 0, size < 2 and size > 2 cases as well, unless you can get your Repository to return a collection type that truly makes that size guarantee for you.

Sign up to request clarification or add additional context in comments.

Comments

2

The best way I could do for index foreach is by using Intstream

List<student> studentList  = studentRepository.findById(idval);

IntStream.range(0, adpodList.size()).forEach(index -> {
            if(index == 0)
            {
 st.setStartTime(studentList.get(index).getStartTime()); // fetch from 1st item
 st.setEndTime(studentList.get(index).getEndTime()); // fetch from 1st item
}
else if(index == 1)
            {
st.setNextStartTime(studentList.get(index).getStartTime()); // fetch from 2nd item
}});

Comments

1

If you know that you have maximum 2 values, you could check if(studentList.size()>1) there are 2 values and set the values of st with studentList.get(0) and studentList.get(1).

I hope it helps.

Comments

1

What about

for (int i=0 ; i<studentList.length-1 ; i++) {
    Student next = studentList.get(i+1);
    studentList.get(i).setNextStartTime(next.getStartTime());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.