Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3981300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:23:37+00:00 2026-05-20T05:23:37+00:00

I have a list of objects and Comparator which is used for sorting and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:23:37+00:00Added an answer on May 20, 2026 at 5:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have list of objects that contain information about which classes static EventHandler should
I have a list of objects, which has a method that has a couple
I have IQueryable list of objects of type T which I want to transform
I have a list of objects which need to be output 2 items per
I have a list of objects which I built with a class, and one
I have a list of specific objects(meetings) which I need to group using a
I have a list of objects in python that I would regularly check and
I have a ViewModel that have a list object containing other list objects and
I have a list of objects I wish to sort based on a field
I have a list of objects, each containing an Id, Code and Description. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.