Example:
package javaapplication1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaApplication1 {
public static void main(String[] args) {
Comparator<Date> comparator = new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
return o1.compareTo(o2);
}
};
List<Date> myList = new ArrayList<>();
myList.add(new Date(2015-1900, 01, 01));
myList.add(new Date(2013-1900, 01, 01));
myList.add(new Date(2013-1900, 01, 02));
myList.add(new Date(2012-1900, 10, 01));
myList.add(new Date(2012-1900, 11, 01));
myList.add(new Date(2015-1900, 01, 10));
Collections.sort(myList, comparator);
//Print all the elements in the List
for(int i = 0; i < myList.size(); i++){
System.out.println(myList.get(i).toString());
}
//find the closest element from the List
Date target = new Date(2013-1900, 01, 02);
int index = Collections.binarySearch(
myList,
target,
comparator);
System.out.println("closest to " + target.toString() + " : " + index);
target = new Date(2013-1900, 01, 03);
index = Collections.binarySearch(
myList,
target,
comparator);
System.out.println("closest to " + target.toString() + " : " + index);
target = new Date(2012-1900, 10, 02);
index = Collections.binarySearch(
myList,
target,
comparator);
System.out.println("closest to " + target.toString() + " : " + index);
}
}
![]() |
| Example of Collections.binarySearch() |

@ java-buddy, do yu mind adding comments to yur code plz, that would make it much more easier to follow and to know the fucntionality of some functions ^^ or sometimes to point referential pages ^^
ReplyDeleteThx again, i've managed to get a code to render the icons in a combobox !!
If yu are intrested, tell me and i'll share the code ^^
hello aymen,
ReplyDeleteThanks for your comment.
I also want to post something about combobox, actually already started. But I prefer try by my own first. Cause I can learn more by myself.