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;
    }

}

7
  • docs.oracle.com/javase/8/docs/api/java/util/… What part of that is unclear? What code did you attempt? Commented Sep 4, 2017 at 1: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? Commented Sep 4, 2017 at 1: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? Commented Sep 4, 2017 at 1:46
  • How do you want to "sort" them, by which order ? and where did you implement this order ? ;) Commented Sep 4, 2017 at 1:47
  • That isn't the method I linked to. Commented Sep 4, 2017 at 1:48

1 Answer 1

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.

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

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.