Java Collections Binary Search : The method binarySearch in the type Collections is not applicable for the arguments -


i trying design ride sharing system. here base object

package rider;  import java.util.treemap;  public class uber{      string driver;     treemap<float,string> destination;      public uber(string d)     {         driver=d;         destination = new treemap<float,string>();     }      private void addtimedest(float tm, string dest)     {         destination.put(tm, dest);     }      float gettsum() {          float tsum=0;          (float f : this.destination.keyset())             tsum+=f;         return tsum;     }  } 

so, each object has driver , associated time<->destination map driver. ultimately, want sort list of such objects time field i.e. key of treemap.

and here iterator class created above

package rider;  import java.util.arraylist; import java.util.collections; import java.util.comparator; import java.util.iterator;  public class uberiterator implements iterator<uber> {      int currindex=0;     arraylist<uber> ulist;     comparator<uber> timecomparator = new comparator<uber>(){           public int compare(uber u1, uber u2) {               return (int) (u1.gettsum()-u2.gettsum());         }           };     public uberiterator(arraylist<uber> nlist)     {         ulist=nlist;         collections.sort(ulist,timecomparator);     }      public boolean hasnext() {          return currindex<ulist.size();     }      public uber next() {         return ulist.get(currindex++);     }      @override     public void remove() {         ulist.remove(currindex--);     }      public void remove(string d) {          int rindex=-1;         for(int u=0 ; u<currindex; u++)         {             if(ulist.get(u).driver.equals(d))             {                 rindex=u;                 break;             }         }          if(rindex<0)             system.out.println("driver not found.");         else         {             ulist.remove(rindex);             currindex--;         }     }      public void remove(float tm) {          int rindex=collections.binarysearch(ulist, tm, timecomparator);          if(rindex<0)         {             system.out.println("exact time not found. closest removed.");          }         else         {             ulist.remove(rindex);             currindex--;         }     }  } 

basically, comparator

comparator<uber> timecomparator = new comparator<uber>(){           public int compare(uber u1, uber u2) {               return (int) (u1.gettsum()-u2.gettsum());         }           }; 

i trying sort key of internal treemap. error

the method binarysearch(list<? extends t>, t, comparator<? super t>) in type collections not applicable arguments (arraylist<uber>, float, comparator<uber>) 

at

int rindex=collections.binarysearch(ulist, tm, timecomparator); 

how should correct implementation?

follow up

is there way override collections.binarysearch ? if uber implements comparable , there define compare method above? shouldn't automatically search time dimension? otherwise benefit of defining custom comparators sorting? sole reason want sort list in way able search efficiently later on.

package rider;  import java.util.treemap;  public class uber implements comparable<uber> {      string driver;     treemap<float,string> destination;      public uber(string d)     {         driver=d;         destination = new treemap<float,string>();     }      private void addtimedest(float tm, string dest)     {         destination.put(tm, dest);     }      public int compareto(uber u) {          return (int) (this.gettsum()-u.gettsum());     }      float gettsum() {          float tsum=0;          (float f : this.destination.keyset())             tsum+=f;         return tsum;     }  } 

int rindex=collections.binarysearch(ulist, tm, timecomparator);

you cannot search float in list<uber>.

your alternatives...frankly aren't great. create fake uber containing tm value , pass collections.binarysearch. use library guava, call lists.transform(ubers, gettmfunction) create view, , pass collections.binarysearch. reimplement binary search yourself.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -