0

I have a dto class which stores some studentid and marks of particular subject. basically like this.

List<StudentInfoDTO> studentInfoDTO = new ArrayList<>();

where StudentInfoDTO is like below

public class StudentInfoDTO {

    Long studentId;
    Short marks;

}

Now I want the student id who has smallest marks.

I tried below but not giving expected result.

int smallest = 0;
for(int i = 0; i < studentInfoDTO.size(); i++) {
    smallest = studentInfoDTO.get(i).getMarks();
    int x = studentInfoDTO.get(i).getMarks();
    if (x < smallest) {
        smallest = x;
    }                       
}
6
  • 1
    You keep re-assigning the smallest variable to the element you're currently processing. Remove that first line of the loop, and initialize smallest to a very large number, then it ought to work. Commented Oct 12, 2018 at 12:12
  • You set smallest to the current marks every loop. Commented Oct 12, 2018 at 12:13
  • well ... what doesn't work? you don't get the Student? that's normal, since you don't save that, all you save is the value of the marks. Also, you are comparing two identical values each iteration, you should set smallest before the for-loop, to the marks of the first Student, and not overwrite that in the loop, unless the marks of that student are smaller Commented Oct 12, 2018 at 12:13
  • Also maintain the id of smallest student because if not later you won't be able to access it from from marks Commented Oct 12, 2018 at 12:14
  • What if several student have the same value for marks? Which would you return? Commented Oct 12, 2018 at 12:15

5 Answers 5

7

You can also use streams, it has a convenient method called min()

studentInfoDTO.stream().min(Comparator.comparing(StudentInfoDTO::getMarks));
Sign up to request clarification or add additional context in comments.

3 Comments

This is a nice solution if you're able to use streams.
@JosephMcCarthy why should anyone not be able to use streams? But anyway, you can use Collections.min(studentInfoDTO,Comparator.comparing(StudentInfoDTO::getMarks)), if you don't like streams.
Some people work on older versions of Java. So if you're stuck with java 1.4 then streams won't help you. Was only pointing that out.
5

You can achieve this multiple ways:

Java 1.4 style:

    StudentInfoDTO smallest = null;
    for (int i = 0; i < studentInfoDTO.size(); i++) {
        StudentInfoDTO current = studentInfoDTO.get(i);
        if (smallest == null || current.getMarks() < smallest.getMarks() ) {
            smallest = current;
        }
    }

Java 5 style:

    StudentInfoDTO smallest = null;
    for (StudentInfoDTO current : studentInfoDTO) {
        if (smallest == null || current.getMarks() < smallest.getMarks()) {
            smallest = current;
        }
    }

Java 8 style:

        StudentInfoDTO smallest = studentInfoDTO.stream()
                                            .min(Comparator.comparing(StudentInfoDTO::getMarks))
                                            .get();

Comments

3

In your code you are assigning smallest again and again while it is meant to have the smallest marks for all time. Also you need to maintain the id for student having smallest marks else you won't be able to access it afterwards.

int smallest = MAX_MARKS;
int id = -1;
for(int i =0; i<studentInfoDTO.size();i++) {
   int x = studentInfoDTO.get(i).getMarks();
   if (x < smallest) {
      smallest = x;
      i = studentInfoDTO.get(i).getStudentId();
   }
}
if(id!=-1){
   System.out.println("id of smallest marks student : " + id);
}

Comments

2
Integer minMarks = studentInfoDTO
      .stream()
      .mapToInt(StudentInfoDTO::getMarks)
      .min().orElseThrow(NoSuchElementException::new);

Comments

0

Problem is you are comparing same x and smallest. So your if condition always fail. This will solve the issue.

int smallest = studentInfoDTO.get(0).getMarks();
for(int i =0; i<studentInfoDTO.size() - 1;i++) 
{
 int x = studentInfoDTO.get(i + 1).getMarks();
 if (x < smallest) {
    smallest = x;
 }
}

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.