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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:38:38+00:00 2026-05-16T05:38:38+00:00

I am trying to write a quick search that searches a List<String> Instead of

  • 0

I am trying to write a quick search that searches a List<String>
Instead of looping through the list and manually checking, I want to do this using binarySearch, but I am not sure how to do it.

Old way:

for(String s : list) {
  if(s.startsWith("contact.")
     return true;
}

Instead I would like something like this:

Collections.sort(list);
Collections.binarySearch(list, FindContactComparator());

Can someone help me write this Comparator?
Is there any better way of doing this instead of using binarySearch?

  • 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-16T05:38:39+00:00Added an answer on May 16, 2026 at 5:38 am

    This should work:

            Comparator<String> startsWithComparator = new Comparator<String>() {
                public int compare(String currentItem, String key) {
                    if(currentItem.startsWith(key)) {
                        return 0;
                    }
                    return currentItem.compareTo(key);
                }
            };
    
    int index = Collections.binarySearch(items, "contact.", startsWithComparator);
    

    However sorting and then binary searching is less efficient than the single pass iteration.

    Addendum:

    Though the above answer helps you, here is another way (inspired from Scala, Google Collections) :

    List<String> items = Arrays.asList("one", "two", "three", "four", "five", "six");
    int index = find(items, startsWithPredicate("th"));
    System.out.println(index);
    
    
    public static Predicate<String> startsWithPredicate(final String key) {
        return new Predicate<String>(){
            @Override
            public boolean apply(String item) {
                return item.startsWith(key); 
            }
        };
    }
    
    public static <T> int find(Collection<T> items, Predicate<T> predicate) {
        int index = 0;
        for(T item: items) {
            if(predicate.apply(item)) {
                return index;
            }
            index++;
        }
        return -1;
    }
    
    interface Predicate<T> {
        boolean apply(T item);
    }
    

    Here the thing is the find() method is not tied with your ‘matching’ logic; it just finds an element that satisfies the predicate. So you could pass on a different implementation of predicate, for ex. which can check ‘endsWith’ to find() method and it would return the found item which ends with a particular string. Further the find() method works for any type of collection; all it needs is a predicate which transforms an element of collection element type to a boolean. This multiple lines of code around a simple logic also show the Java’s lack of support for first class functions.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your terminology is a bit screwed up. Rather than super-… May 16, 2026 at 10:28 am
  • Editorial Team
    Editorial Team added an answer One possibility is to make the plural part of the… May 16, 2026 at 10:28 am
  • Editorial Team
    Editorial Team added an answer What you wrote there is right and I'll add some… May 16, 2026 at 10:28 am

Trending Tags

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

Top Members

Related Questions

Hi I am trying to write a java applet that will get some hard
So I'm trying to write a regex and get it to match the string
I am trying to write a unit test using .NET 4 to ensure that
I'm trying to build a Quicksilver style search system for the internal web app
I'm trying to regex-out a hash given in a string from a web-service response.
I am writing a quick application myself - first project, however I am trying
I'm trying to write two simple macros for begin and end of functions in
I am trying to write a function which can tell me whether there is
I'm trying to write a jQuery script (I have never written one before and
hi i am trying to write a regular expression inside an xslt but 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.