How do i find all objects which match a search cirteria in a binary search?

Posted on 2009-02-15
Last Modified: 2013-11-23
I am using a binary search (collections.binarySearch) to search through a collection for a certain object paratmeter string, now if it exists, it will only find 1 result, but what if there are more objects with that parameter string, how can i print them all?
Question by:area96
    LVL 6

    Expert Comment

    IF you know how many objects are int your list, you can call binarySearch method and then print the values using the index + 1... index + number of objects.

    If you don't know get the index of your first and last object, for example:


          int s = al.indexOf("b"); //here al is an ArrayList
          int f = al.lastIndexOf("b");
            for(int i = s; i <= f; i++) {

    Open in new window

    LVL 92

    Expert Comment

    try subList()

    Collections.binarySearch(list.subList(startIndex, endIndex), ....


    Author Comment

    Thanks for the help guys, It hasnt entirely solved my problem though, as a binary search could drop you anywhere within the block of the array which matches the String that is being searched for, I would need to traverse back through the array until the string does not equal what im lookign for, then work forwards printing out every result..

    Im so close I just need come help with the coding of this..

    Either that or i havent fully understood your advice above.
    Any more help would be greatly appreciated!  Thanks!
    LVL 6

    Accepted Solution

    What you want to do is an inneficient way to achieve your goal. Use the code I wrote above. If you really wanto to use binary search I give you the code.
    al is your List and "c" is the String that you are looking for. Please note that if your String is in the beggining of the List or at the end, it will throw an index Exception. If it is the case you would never should use binarySearch.
    Good luck

        	int index = Collections.binarySearch(al, "c");
        	int cont = index;
        	for(int i = 0; i < al.size(); i++) {
        		if(al.get(index - i) == "c") {
        		} else {
        	for(int i = 0; i < al.size(); i++) {
        		if(al.get(cont + 1) == "c") {
        			System.out.print(al.get(cont + 1));

    Open in new window


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    abstract class with all non abstract mentods 6 58
    triangle challenge 4 41
    parentbit challenge 3 38
    @SBGen Method 3 17
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now