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

  • Home
  • SEARCH
  • 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 240953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:42:45+00:00 2026-05-11T20:42:45+00:00

Is there any way to implement binary search in a ArrayList with objects? In

  • 0

Is there any way to implement binary search in a ArrayList with objects? In this example the ArrayList will be sorted with the field ‘id’.

class User{
 public int id;
 public string name;
}

ArrayList<User> users = new ArrayList<User>();

sortById(users);

int id = 66
User searchuser = getUserById(users,id);

How would the “User getUserById( ArrayList users, int userid )” look like if I it should return the user with a specified id using binary search? Is this even possible?

  • 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-11T20:42:45+00:00Added an answer on May 11, 2026 at 8:42 pm

    The Object Ordering article of The Java Tutorials has an example of writing your own Comparator in order to perform comparisons on custom types.

    Then, the ArrayList (or any other List), the key to find, along with Comparator can be passed into the Collections.binarySearch method.

    Here’s an example:

    import java.util.*;
    
    class BinarySearchWithComparator
    {
      public static void main(String[] args)
      {
        // Please scroll down to see 'User' class implementation.
        List<User> l = new ArrayList<User>();
        l.add(new User(10, "A"));
        l.add(new User(20, "B"));
        l.add(new User(30, "C"));
    
        Comparator<User> c = new Comparator<User>() {
          public int compare(User u1, User u2) {
            return u1.getId().compareTo(u2.getId());
          }
        };
    
        // Must pass in an object of type 'User' as the key.
        // The key is an 'User' with the 'id' which is been searched for.
        // The 'name' field is not used in the comparison for the binary search,
        // so it can be a dummy value -- here it is omitted with a null.
        //
        // Also note that the List must be sorted before running binarySearch,
        // in this case, the list is already sorted.
    
        int index = Collections.binarySearch(l, new User(20, null), c);
        System.out.println(index);    // Output: 1
    
        index = Collections.binarySearch(l, new User(10, null), c);
        System.out.println(index);    // Output: 0
    
        index = Collections.binarySearch(l, new User(42, null), c);
        System.out.println(index);    // Output: -4
                                      // See javadoc for meaning of return value.
      }
    }
    
    class User {
      private int id;
      private String name;
    
      public User(int id, String name) {
        this.id = id;
        this.name = name;
      }
    
      public Integer getId() {
        return Integer.valueOf(id);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 164k
  • Answers 164k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer All paths in Build Settings are assumed relative to the… May 12, 2026 at 12:20 pm
  • Editorial Team
    Editorial Team added an answer You want something that's basically impossible. The way a browser… May 12, 2026 at 12:20 pm
  • Editorial Team
    Editorial Team added an answer It's not well defined. Broadly, as xtofl points out, it's… May 12, 2026 at 12:20 pm

Related Questions

I have a large list of integers (thousands), and I want to extract the
I've been looking around, and so far haven't managed to find a good way
Is there any way to have a binary compiled from an ActionScript 3 project
I have an ActiveX control that works fine in Internet Explorer and the ActiveX

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.