Treeset and Comparator

I am trying to implement a schedule using a TreeSet and a Comparator.
The TreeSet constructor receives a Comparator.
Then I start adding objects that implement an Interface called ScehdulableInterface.
All objects that implement this interface have a method:
Public Date getDate()
The Comparator compare method is defined like this:
public int compare(Object o1, Object o2) {
return ((SchedulableInterface)o1).getEndDate().compareTo(((SchedulableInterface)o2).getEndDate());
My problem is the TreeSet is not accepting objects in the same date. Is there any other Collection that accepts the Comparator interface and accepts objects with the same date ?
Thanks.
Eli

Of course with something like this:public int compare(Object o1, Object o2) {
   // checks for null and other error handling omitted...
   SchedulableInterface si1 = (SchedulableInterface) o1;
   SchedulableInterface si2 = (SchedulableInterface) o2;
   int endDateCompare = s1.getEndDate().compareTo(s2.getEndDate();
   if (endDateCompare == 0) {
       return s1.getSubject().compareTo(s2.getSubject());
   } else {
       return endDateCompare;

Similar Messages

  • TreeSet and Comparator problem

    Hi all ,
    I am using jdk1.3 , jbuilder4.0 , Apache Tomcat 1.3.14 and Window 98 and Access Database.
    I have comparator code below which give me some problem
    public int compare(Object x, Object y) {
    CompanyJB a = (CompanyJB)x , b = (CompanyJB)y;
    double aMinratio = a.getMinratio();
    double bMinratio = b.getMinratio();
    int aCompid = a.getCompanyid();
    int bCompid = b.getCompanyid();
    String aRankstr = a.getRank();
    String bRankstr = b.getRank();
    int tmp;
    tmp = new Integer(aCompid).compareTo(new Integer(bCompid));
    if (tmp == 0) {
    return tmp;
    tmp = a.getRank().compareTo(b.getRank());
    if (tmp !=0) {
    return tmp;
    tmp = new Double(a.getMinratio()).compareTo(new Double(b.getMinratio()));
    if (tmp !=0) {
    return tmp;
    return new Double(a.getMinratio()).compareTo(new Double(b.getMinratio()));
    Client source code below :
    TreeSet list2 = new TreeSet(new test());
    //list2.add(new test(2,1.9,4.2,"NA")); // 1
    list2.add(new test(11,8.5,8.5,"2")); // 3
    list2.add(new test(1,0.6,0.6,"NA")); // 2
    list2.add(new test(11,2.5,2.3,"NA")); // 4
    list2.add(new test(12,0.3,0.3,"1")); // 5
    list2.add(new test(2,1.9,1.2,"NA"));
    list2.add(new test(12,0.9,0.9,"NA"));
    Iterator iterator2 = list2.iterator();
    while(iterator2.hasNext()){
    System.out.println(((test)iterator2.next()).getCompanyid());
    My problem is that the print out display duplicate company id "12" in the set.
    It should not display the duplicate company id in the set.
    What is the problem here ?? Anybody have ideas ??

    Actually, a Set, as defined in java.util.Set, should not contain duplicate members. With SortedSets, however, there must also be consistency between equals() and the compareTo() (or Comparator.compare()). In other words, equal objects must sort to the same or an adjacent place in order for the Set semantics to work.
    In your example, "equal" objects can appear in completely different positions in the sort order, throwing off this consistency. When adding, it's traversing the tree in Rank,MinRatio order. If by chance it runs across a node that has the same company id, it stops and leaves the trees alone (as it does with id=11). If you try graphing the tree out by hand using your compare() as left/right criteria (negative left, positive right, 0 don't change), it'll become immediately clear what's happening.
    Also of interest: you'll find that you can't add test(34,1.9,1.2,"NA") to the list: because it's Rank and MinOrder is the same as the id=2 entry, it'll never get added.

  • TreeSet and Comparable

    Say I have a class called Player which implements Comparable. And I want to put the Player object into a TreeSet. Inside the Player class I have a compareTo method. Will then the TreeSet be automatically use the compareTo method inside the Player class to put the Player elements inside the TreeSet??

    the weird tning is that whenever I tried to erase the
    compareTo method on the Player class, I can't
    add/remove player from the tree setRead the docs. You have to either provide elements that implement Comparable, or you have to provide a Comparator. Ignore reply 1. He doesn't know what he's talking about.

  • Date fields and compare and get the later date of the fields

    I am trying to compare these date fields and compare and get the later date of the fields
    Tables are
    TABCASER
    TABCASER1
    EVCASERS
    Field
    Are
    TABCASER1.CASER_no
    The dates are to be compared and then get the records with the highest or latest date value.
    TABCASER1.CASERRECIEVEDDATE
    EVCASERS.FINALEVDATES
    EVCASERS.PUBLICATIONDATE
    EVCASERS.PUBLICATIONDATE
    TABCASER.COMPAREACCEPDATE
    I have this code but I am trying to figure out what it all means.
    I have several questions.
    1.
    1.     greatest it is used here to compare right? How do I then output this ? do I store it to a var (coldfusion) ultimately , I wish to send it to a page of records
    2.     is it necessary to use todate? And to_date? What does this do?
    3.     decode, is this necessary too. What does this do? NULL?
    4.     
    5.     
    6.     when I do get the query results how do I send it to coldsuion and out put to a display.
    Someone sent me this code.
    is there abetter way of doing this? To compare the dates and store in a var to display. thanks
    Here is my code below:
    Greatest(
         CASE
              WHEN INSTR(TABCASER1.CASER_no,'-CE') > 0 THEN
         decode(TABCASER1.CASERRECIEVEDDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),TABCASER1.CASERRECIEVEDDATE)
                             WHEN INSTR(TABCASER1.CASER_no,'-ERNIE') > 0 THEN
         decode(EVCASERS.FINALEVDATES,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),EVCASERS.FINALEVDATES)
                             WHEN INSTR(TABCASER1.CASER_no,'-MONIE') > 0 THEN
         decode(EVCASERS.PUBLICATIONDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),EVCASERS.PUBLICATIONDATE)
                             WHEN INSTR(TABCASER1.CASER_no,'-NADINE') > 0 THEN
         decode(EVCASERS.PUBLICATIONDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),EVCASERS.PUBLICATIONDATE)
                             ELSE
         decode(TABCASER.COMPAREACCEPDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),TABCASER.COMPAREACCEPDATE)
                        END
              ,decode(TABCASER.COMPAREACCEPDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),TABCASER.COMPAREACCEPDATE))
              between TO_DATE('#dateformat(form.startDate,"mm/dd/yyyy")#','MM/DD/YYYY') and TO_DATE('#dateformat(form.endDate,"mm/dd/yyyy")#','MM/DD/YYYY')
    is there abetter way of doing this? To compare the dates and store in a var to display. thanks

    Hi
    If you have date datatypes than:
    select
    greatest(TABCASER1.CASERRECIEVEDDATE, EVCASERS.FINALEVDATES, EVCASERS.PUBLICATIONDATE, EVCASERS.PUBLICATIONDATE, TABCASER.COMPAREACCEPDATE)
    from TABCASER, TABCASER1, EVCASERS
    where ...-- join and other conditions
    1. greatest is good enough
    2. to_date creates date dataype from string with the format of format string ('mm/dd/yyyy')
    3. decode(a, b, c, d) is a function: if a = b than return c else d. NULL means that there is no data in the cell of the table.
    6. to format the date for display use to_char function with format modell as in the to_date function.
    Ott Karesz
    http://www.trendo-kft.hu

  • How to import a image and compare with another image to find their deviation

    i am new to labview , can anyone tell how to import a image and compare  it with another image  to find their deviation
    Solved!
    Go to Solution.

    This is an quite easy task using the Vision Development Module.
    There you can acquire images from cameras or read them form the harddrive, then differntiate them or since VDM 2010 calculate the optical flow between consecutive images.
    Christian

  • Excise duty caluclate and compare between Amount (JEXQ ) and JEXP

    Dear all,
    We are implementing SAP SD module for cement industry
    So we have special requirement Regarding to Excise Duty
    BED     JEXQ    IS      600 ( with respect to price value)
                JEXP    IS      12% ON MRP.( it should calculate on MRP price)
    For this we have two pricing condition types
    one is normal price
    another one is MRP ( it is statical)
    both should calculate  and compare between both,
    and finally value is not more then 600
    We are using TAXINN  Pricing procedure,
    please suggest me for solution ,
    Regards,
    GANGI

    Hi
    Can u pls chk in your pricing procedure weather the stastics chk box is ticked for the respective condition type.
    If so pls remove and try again.
    Vijay

  • Register and Compare Time

    Hello people,
    I want to register the time into a variable when a user clicks into a button and, when he clicks again, compare the time with the last time (registered into a variable).
    How do I save time (hh:mm:ss) into a var and compare with another var?
    Thank's.

    http://java.sun.com/javase/6/docs/api/java/util/Date.html#after(java.util.Date)
    http://java.sun.com/javase/6/docs/api/java/util/Date.html#before(java.util.Date)

  • Project Documentation tab: can you 'freeze' a graph so another can be added and compared against?

    I've been using the Project Documentation view recently to quickly compare dc signals against changes that I make to an electrical system. What I'm trying to do is capture a few seconds of a DC signal, stop acquisition, make a change in the system, then capture a few more seconds of the same signal and compare the two.
    Project Documentation view is nice because I can drag the graphs onto the document for quick on the fly comparison.
    Problem is, I drag a acquired signal's graph onto the document expecting the waveform to 'freeze' so that I can capture another segment of data and drag that graph onto the document for comparison. But once I acquire this second burst of data the first graph captures the live waveform, overwriting the baseline waveform I'm trying to compare against.
    Is there a way to screencapture or freeze the graph so that multiple graphs can be stacked within the document form without them all updating?
    SCXI- 1000 Chassis w/ 1346 adapter
    PCI 6281 DAQ card
    SCXI- 1520 Bridge Board w/ 1314 Terminal Block (x2)
    SCXI- 1180 Feedthrough Panel w/ 1302 Block
    Signal Express 2014.
    Win7 Enterprise

    Hi OKors,
    I’m not sure if you can do exactly what you want. If it were me, I would take the first reading, then save it to an LVM file, then take the second reading, store that to an LVM file, then load the files to compare.
    TimC
    National Instruments
    Applications Engineer

  • Convert char to quantity(15) and compare

    Hi Expert!,
           I have to convert Char30 format to quantity15 format and compare those to value.
          I have written code below . is the right ? is there any
    data: x_objdata_ausp_1 TYPE char30,
           l_ausp_1          type p decimals 3,
           l_vbap_kwmeng     type p decimals 3,
           x_vbap_kwmeng     type vbap-kwmeng.
    x_objdata_ausp_1 = '1.500'.  "char30 type data
    move x_objdata_ausp1 to l_ausp_1.
    l_ausp_1 = l_ausp_1 * 1000.
    x_vbap_kwmeng  = '1600.000'.   "quan(15) type data.
    move x_vbap_kwmeng to l_vbap_kwmeng.
       if l_vbap_kwmeng > l_ausp_1.
          write: 'item quantity is greater than characteristic value'.
    else.
          write: 'Item quantity is less than characteristic value'.
       endif.

    Hi Mithilesh,
    I have checked your code and it's working fine.
    you can use it.It's good that your converting both variables in  packed.
    Thanks.
    Anurag

  • RFC comparing Purchase Order from server D10 and compare the PO E10 server

    Hi Gurus,
    I want to fetch data of Purchase order from D10 system client 058 and compare whether the same Purchaser order is also present in E10 system clilent 078.
    Please send me the sample code how to retrieve the code from different servers using RFC. I am new to RFC, please guide me same code.
    Regards,
    Prasad
    <REMOVED BY MODERATOR>
    Edited by: Alvaro Tejada Galindo on Jan 30, 2008 4:24 PM

    Hello Arza
    You can use BAPI BAPI_PO_GETDETAIL to retrieve the PO data from the two (remote)systems.
    During an important migration project I had to compare whether the PO data were the same on the old and new SAP system.
    For this purpose I used the BAPI to retrieve the PO data, initialized all data of which I new they would differ between the old and the new system (e.g. time stamps, date, time).
    Next I defined a complex structure which contained all PO data that I wanted to compare.
    Each PO gave one record which I collected into an itab.
    Finally I transformed the itab data into a single XML file for the data from the old and the new system. Then I took an XML editor (e.g. Oxygen) and made a diff analysis of the two XML files which showed me every difference.
    This way I was able to compare thousands of PO's within a few hours.
    Regards
      Uwe

  • How to get count,index and compare to arraylists

    hi
    how to get count,index and comparing of 2 arraylist.... plz suggest me asap...

    How is your question related to JDBC? And what have you done so far with your code?

  • Whats the difference between comparable and comparator?

    whats the difference between comparable and comparator?
    when must i use comparable, and when must i use comparator?

    whats the difference between comparable and
    comparator?Comparable is from the java.lang package, Comparator from java.util.
    when must i use comparable, and when must i use
    comparator?Here's a tutorial on both:
    http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

  • I want to make 2 thesaurus and compare between them?????????Help

    Hi everybody,
    I have a Problem,I want to make 2 thesaurus and compare between them.
    I mean : I have a File Like This-----with lexicographical Order----
    ENG GERMAN
    Any Jede
    I ich
    Two Zwei
    when i write a sentence an german the Program must look the synonym of English an write it.
    Please help me as soon as possible
    Thanks

    Cross posted:
    http://forum.java.sun.com/thread.jspa?threadID=5259185 http://forum.java.sun.com/thread.jspa?threadID=5259240

  • Query regarding implementation of HashSet and TreeSet and AbstractSet

    I have found that the treeSet and HashSet extends AbstractSet and implements Cloneable and Serializable interfaces.
    What happens if AbstractSet itself implements Cloneable and Serializable interfaces, so that The HashSet and TreeSet need not to implement separately?
    Is it a design concern or is there any logic behind in doing so?
    I appreciate your quick response..
    Thanks
    Shiva kumar

    It would mean that anybody else such as me who extended AbstractSet would be stuck with my implementation being serializable and cloneable whether I wanted or not. No doubt the designers wanted to provide the freedom not to do so.

  • Comparable and comparator interface in java

    Hi All,
    How comparable and comparator interface works in java and when to use comparable and when to use comparator.please give me some example(code) as I am not able to understand the difference.
    Thanks
    Sumit
    Edited by: sumit7nov on May 17, 2009 4:45 AM

    Thanks,one more doubt.
    We have Collections.sort() method and we can sort any list by this method without implementing comparable or comparator interface.Then my question is when we need to implement comparable or comparator interface or when do we write our own compareTo() or compare() methods.
    Thanks
    Sumit

Maybe you are looking for