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 4027180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:03:52+00:00 2026-05-20T11:03:52+00:00

I am storing objects in ArrayList, where my pojo is as public class POJOSortableContacts

  • 0

I am storing objects in ArrayList, where my pojo is as

public class POJOSortableContacts {
    private Long id;
    private String displayName;


    public POJOSortableContacts(Long id, String displayName) {

        super();
        this.id           = id;
        this.displayName  = displayName;
    }

    //Setter and Getters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    //This will be used to sectioned header.
    public String getLabel() {
        return Character.toString(displayName.charAt(0)).toUpperCase();
    }



    //Sortable categories
    //Sort by Contact name
    public static Comparator<POJOSortableContacts> COMPARE_BY_NAME = new Comparator<POJOSortableContacts>() {
        public int compare(POJOSortableContacts one, POJOSortableContacts other) {
            return one.getDisplayName().compareToIgnoreCase(other.getDisplayName());
            //return s1.toLowerCase().compareTo(s2.toLowerCase()); //it returns lower_case word first and then upper_case
        }
    };

    //Sort by id
    public static Comparator<POJOSortableContacts> COMPARE_BY_ID = new Comparator<POJOSortableContacts>() {
        public int compare(POJOSortableContacts one, POJOSortableContacts other) {
            return one.id.compareTo(other.id);
        }
    };
}

and Arraylist structure is as

ArrayList<POJOSortableContacts> contactArrayList = new ArrayList<POJOSortableContacts>() 

, I want to search an object from contactArrayList by id (for example I want an object which id is 20), I want to use binarysearch for this. So how can it will be?

  • 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-20T11:03:53+00:00Added an answer on May 20, 2026 at 11:03 am

    You can use

    POJOSortableContacts contact = Collections.binarySearch(contactArrayList,
                                                   new POJOSortableContacts(20, ""),
                                                   COMPARE_BY_ID);
    

    The new POJOSortableContacts is just a dummy object to act as the key.

    Of course, this will only work if your list is sorted by ID to start with – you can’t use a binary search on an unsorted list (or on a list which is sorted in a different way).

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

Sidebar

Related Questions

//Activity-- mTweetApp.SetCaseInfo(type,teet); //storing in another class public void SetCaseInfo(String PatientType,ArrayList arr){ // All objects
My Hashmap as declared as HashMap<String, ArrayList<SortableContactList>> where SortableContactList list is a POJO class
I have a class along the lines of: public class Observation { private String
I created an ArrayList which contains KeyValuePair<string, string> objects. There are ListBoxes which I
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
public ClassA { private String firstId; private String secondId; public void setFirstId(String firstId) {
If a collection, like an arraylist, will be storing custom objects (eg Person with
I have an ArrayList of objects being dumped to a YAML string and have
I have an ArrayList of objects where each object contains a string 'word' and
I have an ArrayList of Foo objects. Foo's properties are String name and int

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.