0

I have created ArrayList and I want to search dog details by registration number using binary search. I tried using Collections.binarySearch but could not figure it out. How can I search dog details like name and breed using registration number?

DogSort.java

public class DogSort {

    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
    ArrayList<Dog> listDog = new ArrayList<Dog>();

    listDog.add(new Dog("Max", "German Shepherd", "1001"));
    listDog.add(new Dog("Gracie","Rottweiler","1003"));
    listDog.add(new Dog("Sam", "Beagle", "1002"));

    }
}

Dog.java

class Dog {
    private String name;
    private String breed;
    private String registrationNumber;


    public Dog(String name, String breed, String registrationNumber) {
        this.name = name;
        this.breed = breed;
        this.registrationNumber = registrationNumber;
    }


    public String getName() {
        return this.name;
    }

    public String getBreed() {
        return this.breed;
    }

    public String getRegistrationNumber() {
        return this.registrationNumber;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBreed(String breed) {
        this.breed = breed;
    }

    public void setRegistrationNumber(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    @Override
    public String toString() {
        return this.name;
    }

}

Jayveer Parmar
  • 500
  • 7
  • 25
  • https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#binarySearch-java.util.List-T-java.util.Comparator- What part of that is unclear? What code did you attempt? – markspace Sep 04 '17 at 01:43
  • I have tried reading Oracle doc and then I posted question. If I don't understand from doc, shouldn't I post question here? – Jayveer Parmar Sep 04 '17 at 01:44
  • I have tried `Collections.binarySearch(listDog,"1002");` but it's not gonna work as I know that it should match with parameter of Dog class but I don't know how to search only by one parameter? – Jayveer Parmar Sep 04 '17 at 01:46
  • How do you want to "sort" them, by which order ? and where did you implement this order ? ;) – Nir Alfasi Sep 04 '17 at 01:47
  • That isn't the method I linked to. – markspace Sep 04 '17 at 01:48
  • @alfasin I have added three entry in `listDog` as shown in `DogSort.java`. I want to search them by registration number in any order. – Jayveer Parmar Sep 04 '17 at 01:49
  • Possible duplicate of [How to implement the Java comparable interface?](https://stackoverflow.com/questions/21626439/how-to-implement-the-java-comparable-interface) – Nir Alfasi Sep 04 '17 at 01:51

1 Answers1

1

The Collections#binarySearch() method accepts a list of objects which extend the Comparable interface, and a key, and returns the index of the key in the list if found. The major problem with your code is that need to make your Dog class comparable, something like this:

public class Dog implements Comparable<Dog> {
    private String name;
    private String breed;
    private String registrationNumber;

    public Dog(String name, String breed, String registrationNumber) {
        this.name = name;
        this.breed = breed;
        this.registrationNumber = registrationNumber;
    }

    @Override
    public int compareTo(Dog dog) {
        if (dog == null) return 1;

        if (this.registrationNumber == dog.registrationNumber) return 0;

        return this.registrationNumber > dog.registrationNumber ? 1 : -1;
    }
}

If you keep reading the Javadoc, you will see this:

The list must be sorted into ascending order according to the natural ordering of its elements (as by the sort(List) method) prior to making this call.

Binary search will only work, or at least in a behaved way, if your list of dogs is already sorted in ascending order. In this case, we have overriden the natural order to sort according to registration number.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360