John Smith
John Smith

Reputation: 55

Java Collections.binarySearch() returns null

I have a list of objects and Comparator which is used for sorting and serching in that list. Collections.binarySearch() return null while it supposed to return an integer value. Here is the code :

List<AttribRow> rows =  new ArrayList<AttribRow> ();
AttribRow temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)23);
rows.add(temp);
temp = new AttribRow();
temp.setIndv1((long)22);
rows.add(temp);

temp = new AttribRow();
temp.setIndv1((long)25);
temp.setId((long)55);
Collections.sort(rows, new CitRowsComparator());
int index = 0;
index = Collections.binarySearch(rows, temp,new CitRowsComparator()); 

AttribRow is entity bean class mapped to the table. It has a field indv1 which is used in comparison.

private Long indv1;
public Long getIndv1() {
    return indv1;
}

public void setIndv1(Long indv1) {
    this.indv1 = indv1;
}

This is a code for Comporator class

public class CitRowsComparator implements Comparator<AttribRow> {
    public CitRowsComparator() {
        super();
    }

    public int compare(AttribRow one, AttribRow two) {
        return one.getIndv1().compareTo(two.getIndv1());
    }
}

Collections.binarySearch() alsways returns null. Even when I changed compare() method in Comparator to return 0 it was still returning null. Object temp thaht used as a key is also null after binarySearch envocation. I don't ge any expetions. I tried the same code for other class with one field and it worked fine. Any help will be appreciated.

Upvotes: 0

Views: 2739

Answers (3)

卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32004

Note: AttribRow with Indv1 25 is not added to list, your code is missing rows.add(temp); after temp.setIndv1((long) 25);.

Additionally, I think re-assign Object temp = new AttribRow() is error-prone practice.

Upvotes: 0

Torres
Torres

Reputation: 5400

I´m getting -6 with your code too...and why you use so many 22 Indv1? according to this you will have the doubt of which object is the correct:

Searches the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the specified comparator (as by the Sort(List, Comparator) method, above), prior to making this call. If it is not sorted, the results are undefined. If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found.

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308061

The return type of Collections.binarySearch() is int so this method can't ever return null. It might return 0, but that would be an indication that the object is found at index 0 (i.e. the first element).

Also, there's no way that temp is null after your call to Collections.binarySearch() since that method can't modify the value of temp (as Java is not pass-by-reference).

When I try your code index is -6 after binarySearch returns, indicating that temp is not in the collection (which is expected, as no AttribRow object with indv1 value 25 is in there) and would be inserted at position 6 to be sorted.

Upvotes: 5

Related Questions