-1

I want to write a program such that:

1) A class Student (with name, id, batch)

2) In that class, write a int compareByBatch(Student s1, Student s2) method

3) Use collections.sort() to compare objects

4) Use the compareByBatch method as the second argument to the sort method.

I am able to write the program as follows:

Student.java class file:

public class Student {
    private String id;
    private String name;
    private int Batch;

    //Added a class constructor getters and setters

     public static Comparator<Student> studBatch = new Comparator<Student>() {

            public int compare(Student s1, Employee s2) {

               int batch1 = s1.getBatch();
               int batch2 = s2.getBatch();

               //For ascending order
               return batch1-batch2;
           }};
}

Then, in the Main file added the following:

Created an ArrayList and added records in it:

ArrayList<Student> stud = new ArrayList<>();

stud.add(new Student("A01", "John Doe", 101));

stud.add(new Student("A02", "Mike Gaman", 102));

Collections.sort(stud, stud.studBatch);

The above code works fine and I get the sorted records by Batch.

But, my question is, how to write a code as per my coding requirements which I have mentioned in the beginning i.e. using compareByBatch() method inside Student class and using in with Collections.sort()? I need to pass a method reference from object class as the second argument for the Collections.sort().

Cœur
  • 37,241
  • 25
  • 195
  • 267
Amit Jathar
  • 319
  • 3
  • 14
  • 2
    `Collections.sort(stud, Student::compareByBatch)`? Really, you should ask the person giving you the requirements for more information. We can only guess. Voting to close as unclear. – Seelenvirtuose Feb 22 '18 at 07:06
  • It is a valid scenario, please check the solution given in the next post, that worked for me! – Amit Jathar Feb 23 '18 at 06:25
  • Whether an "answer" helps you or not is not relevant for the decision whether this "question" fits on SO or not! – Seelenvirtuose Feb 23 '18 at 09:48

1 Answers1

2

I suppose the purpose of this exercise is for you to learn to use method reference syntax.

If you look at Collections.sort, it requires a Comparator<T> as the second parameter. This Comparator<T> can actually be represented by a method of the signature int compare(T obj1, T obj2). This is one of those functional interfaces.

So instead of passing it an implementation of Comparator<T> as an object, you can just pass it a method reference.

Collections.sort(stud, Student::compareByBatch);

Notice how I used ::. That is the syntax you need to use to denote a method reference.

And your compareByBatch method can just be like this:

public static int compareByBatch(Student s1, Student s2) {
    int batch1 = s1.batch;
    int batch2 = s2.batch;
    return batch1-batch2;
}

By the way, your compareByBatch is not a very good implementation. You should probably check for nulls first and use Integer.compare(batch1, batch2) instead of just subtracting them. See here for why.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • It works!! Thanks for your help!! :) – Amit Jathar Feb 23 '18 at 06:24
  • @sweeper how does the static compareByBatch() related to the compare() method of the Comparator? The end it still has to be related to Comparator interface which is method has no relation. Please advise. Tx – yapkm01 Aug 22 '23 at 01:55
  • @yapkm01 The method reference `Student::compareByBatch` has the same *function type* as the function type of the `Comparator` interface. Namely, both of them represent functions that take two `Student` parameters and return an `int`. Therefore, the method reference expression `Student::compareByBatch` can be passed to a method expecting a `Comparator`. – Sweeper Aug 22 '23 at 01:59
  • @Sweeper If i understand you correctly what you are saying is the compareBatch() is a replacement for the Comparator compare() method? Also in your case there is no Comparator function object created, right? – yapkm01 Aug 22 '23 at 02:25
  • 1
    @yapkm01 There *is* an object being created. At runtime, the method reference expression `Student::compareByBatch` creates an object that implements `Comparator`. That object's `compare` method is implemented by calling `compareByBatch`. – Sweeper Aug 22 '23 at 02:28
  • @sweeper .. OK. So that confirm my suspicion. It is the compare() internally call the static Student.compareBatch(). Did I get it right? – yapkm01 Aug 22 '23 at 02:35
  • @yapkm01 Yeah. See also the [JLS](https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.13) for more info. – Sweeper Aug 22 '23 at 02:38
  • @sweeper TX so much – yapkm01 Aug 22 '23 at 02:49