Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2385
  • Last Modified:

Java BinarySearch Comparator Example

I'm trying to find an example of a binarysearch comparator that finds a string in an arraylist.  I need the compareTo() example as well.  Appreciate it.
0
ihateuselessitbooks
Asked:
ihateuselessitbooks
  • 6
  • 3
6 Solutions
 
Thomas4019Commented:
Here is an example showing a binary search on an ArrayList.

http://www.java-examples.com/perform-binary-search-java-arraylist-example
0
 
Thomas4019Commented:
That example uses the built in Collections.binarySearch()
0
 
ihateuselessitbooksAuthor Commented:
been there, done that.  I'm after the Comparator and compareTo(), not the call to the comparator.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Thomas4019Commented:
The class java "Comparator" is designed for you to specify a way of comparing objects. Such as that you could make a CustomComparator which has its own system for deciding whether object A is less than or greater than object B.

Or if you are making a custom object you can implement the "Comparable" interface. This mandates that you supply the function that tells whether an object is less than or greater than another object.

There is no such thing as a "Binary Search Comparator" if you are talking about comparators as they are used in Java. You could make a class which does binary searching named Comparator, of course, but you can not do a binary search with only comparators.

Comparators and Comparable are only designed for specifing the mehod to compare objects.

http://www.onjava.com/pub/a/onjava/2003/03/12/java_comp.html
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
0
 
ihateuselessitbooksAuthor Commented:
thanks for your help, but still looking here.
0
 
Thomas4019Commented:
what exactly are you looking for?
0
 
Thomas4019Commented:
Java custom implementaion of a binary search. (not using Collections.binarySearch())

http://www.java-tips.org/java-se-tips/java.lang/binary-search-implementation-in-java.html
0
 
Thomas4019Commented:
0
 
ihateuselessitbooksAuthor Commented:
Thanks for the help.  Actually looking for an example of a binarySearch comparator called on the order of Collections.sort.   Being somewhat new in Java, I probably didn't even ask the right question(s) completely, but I did figure out something after pulling my hair out a few days.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now