I am able to find first using the Collections.binarySearch but how to find the last position of element. Thank you in advance:)
List < String > nums = new ArrayList < String > ();
nums.add("A/tea");
nums.add("C/see");
nums.add("C/sEa");
nums.add("C/SEa");
nums.add("C/clock");
nums.add("aep");
Collections.sort(nums,String.CASE_INSENSITIVE_ORDER);
System.out.println(nums);
int pos = Collections.binarySearch(nums,"C/SEA",String.CASE_INSENSITIVE_ORDER);
CodePudding user response:
Try something like this:
public static <T> int findLast(List<T> list, T search, Comparator<T> comp) {
int pos = Collections.binarySearch(list, search, comp);
if (pos >= 0) {
while (pos 1 < list.size()
&& comp.compare(search, list.get(pos 1)) == 0) {
pos;
}
}
return pos;
}