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;
    }                       
}
Feedforward
  • 4,521
  • 4
  • 22
  • 34
Anjali
  • 1,623
  • 5
  • 30
  • 50
  • 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. – Henrik Aasted Sørensen Oct 12 '18 at 12:12
  • You set smallest to the current marks every loop. – aydinugur Oct 12 '18 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 – Stultuske Oct 12 '18 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 – jack jay Oct 12 '18 at 12:14
  • What if several student have the same value for marks? Which would you return? – jpw Oct 12 '18 at 12:15
  • Related: [How do you find the smallest value of an ArrayList?](https://stackoverflow.com/questions/36211028/how-do-you-find-the-smallest-value-of-an-arraylist) – Ole V.V. Oct 12 '18 at 12:35

5 Answers5

7

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

studentInfoDTO.stream().min(Comparator.comparing(StudentInfoDTO::getMarks));
reebow
  • 525
  • 1
  • 4
  • 15
  • This is a nice solution if you're able to use streams. – Joseph McCarthy Oct 12 '18 at 13:04
  • @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. – Holger Oct 14 '18 at 20:21
  • 1
    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. – Joseph McCarthy Oct 14 '18 at 20:27
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();
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);
}
jack jay
  • 2,493
  • 1
  • 14
  • 27
2
Integer minMarks = studentInfoDTO
      .stream()
      .mapToInt(StudentInfoDTO::getMarks)
      .min().orElseThrow(NoSuchElementException::new);
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
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;
 }
}
abo
  • 378
  • 4
  • 19