Comparator API specs lies...

Dear All
whilst revising for my scjp1.4, which incidetally is taking place tomorrow 06/10/03, I stumbled across what I might define a mistake ( bug is too pretentious ) in the Comparator API specs.
I'd like to quote:
"For example, if one adds two keys a and b such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) to a sorted set with comparator c, the second add operation will return false (and the size of the sorted set will not increase) because a and b are equivalent from the sorted set's perspective. "
Nothing could be more false than such a statement. I'd invite you to belie my words.
Following is a program that should demonstrate my reasoning:
import java.util.*;
public class OrderingTest {
   public static void main(String[] args) {
      if(args.length != 4) {
         usage();
         System.exit(1);
      int arg1, arg2, arg3, arg4;
      arg1 = Integer.parseInt(args[0]);
      arg2 = Integer.parseInt(args[1]);
      arg3 = Integer.parseInt(args[2]);
      arg4 = Integer.parseInt(args[3]);
      Element e1 = new Element(arg1, arg2);
      Element e2 = new Element(arg3, arg4);
      ElementComp ec = new ElementComp();
      System.out.println("e1: " + e1);
      System.out.println("e2: " + e2);
      Set set1 = new HashSet();
      Set set2 = new TreeSet(); // natural order through the comparable nature of Element
      Set set3 = new TreeSet(ec); // total order by passing a comparator
      set1.add(e1);
      set1.add(e2);
      System.out.println("HashSet: " + set1);
      set2.add(e1);
      set2.add(e2);
      System.out.println("TreeSet: " + set2);
      set3.add(e1);
      set3.add(e2);
      System.out.println("TreeSet: " + set3);
   public static void usage() {
      System.out.println("You need to provide 4 arguments: the first two will passed to e1 constructor, the last two will passed to e2 constructor");
class Element implements Comparable {
   public int i;
   public int j;
   public Element(int i, int j) {
      this.i = i;
      this.j = j;
   public boolean equals(Object o) {
      if(o == null) return false;
      Element e = (Element) o;
      boolean result = i == e.i;
      System.out.println(this + ".equals(" + o + "): " + result);
      return result ;
   public int hashCode() {
      System.out.println( this + ".hashCode() called !");
      return 31 * i;
   public String toString() {
      return "Element@" + super.hashCode() + ": " + i + " " + j;
   public int compareTo(Object o) {
      Element e = (Element) o;
      int result = j == e.j ? 0: j - e.j;
      System.out.println(this + ".compareTo(" + o + "): " + result);
      return result;
class ElementComp implements Comparator {
   public int compare(Object o1, Object o2) {
      Element e1 = (Element) o1;
      Element e2 = (Element) o2;
      int result = e1.j == e2.j ? 0: e1.j - e2.j;
      System.out.println("Comparator.compare(" + o1 + ", " + o2 + "): " + result);
      return result;
}You will find 3 sets: a HashSet, a TreeSet, which uses the comparable nature of Element, and another TreeSet, which avails itself of a comparator in order to sort its elements.
What I would like to prove by this is the following:
HashSet makes use of hashCode() and where necessary of equals(Object o) to make store and to check upon the equality of object.
TreeSet with Comparable does not make use of hashCode() and equals(Object o), but only compareTo(Object o) to store object and and check for their equality.
TreeSet with Comparator does not make use of hashCode() and equals(Object o), bu only compare(Object o1, Object o2) to store object and and check for their equality.
The output of the program demonstrates the last 3 statements.
Now to go back to what the Comparator specs says, if you add two object such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) you will notice that the second element is indeed added to the Set. I have made sure that hashCode() has been redefined to be consistent with equals(Object o) and that compareTo(Object o) and compare(Object o1, Object o2) to be inconsistent with equals.
Furthermore, have a look at the Comparator API 's counterpart, Comparable; you will find a similar statement, but this time it's correct!
Now I am eager to hear from you, all the more since tommorrow I am sitting the scjp 1.4.
thanks for your help in advance
luca iacono

I answered in the other forum.
Please do not crosspost.

Similar Messages

  • Comparator API mistake ???

    Dear All
    whilst revising for my scjp1.4, which incidetally is taking place tomorrow 06/10/03, I stumbled across what I might define a mistake ( bug is too pretentious ) in the Comparator API specs.
    I'd like to quote:
    "For example, if one adds two keys a and b such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) to a sorted set with comparator c, the second add operation will return false (and the size of the sorted set will not increase) because a and b are equivalent from the sorted set's perspective. "
    Nothing could be more false than such a statement. I'd invite you to belie my words.
    Following is a program that should demonstrate my reasoning:
    import java.util.*;
    public class OrderingTest {
       public static void main(String[] args) {
          if(args.length != 4) {
             usage();
             System.exit(1);
          int arg1, arg2, arg3, arg4;
          arg1 = Integer.parseInt(args[0]);
          arg2 = Integer.parseInt(args[1]);
          arg3 = Integer.parseInt(args[2]);
          arg4 = Integer.parseInt(args[3]);
          Element e1 = new Element(arg1, arg2);
          Element e2 = new Element(arg3, arg4);
          ElementComp ec = new ElementComp();
          System.out.println("e1: " + e1);
          System.out.println("e2: " + e2);
          Set set1 = new HashSet();
          Set set2 = new TreeSet(); // natural order through the comparable nature of Element
          Set set3 = new TreeSet(ec); // total order by passing a comparator
          set1.add(e1);
          set1.add(e2);
          System.out.println("HashSet: " + set1);
          set2.add(e1);
          set2.add(e2);
          System.out.println("TreeSet: " + set2);
          set3.add(e1);
          set3.add(e2);
          System.out.println("TreeSet: " + set3);
       public static void usage() {
          System.out.println("You need to provide 4 arguments: the first two will passed to e1 constructor, the last two will passed to e2 constructor");
    class Element implements Comparable {
       public int i;
       public int j;
       public Element(int i, int j) {
          this.i = i;
          this.j = j;
       public boolean equals(Object o) {
          if(o == null) return false;
          Element e = (Element) o;
          boolean result = i == e.i;
          System.out.println(this + ".equals(" + o + "): " + result);
          return result ;
       public int hashCode() {
          System.out.println( this + ".hashCode() called !");
          return 31 * i;
       public String toString() {
          return "Element@" + super.hashCode() + ": " + i + " " + j;
       public int compareTo(Object o) {
          Element e = (Element) o;
          int result = j == e.j ? 0: j - e.j;
          System.out.println(this + ".compareTo(" + o + "): " + result);
          return result;
    class ElementComp implements Comparator {
       public int compare(Object o1, Object o2) {
          Element e1 = (Element) o1;
          Element e2 = (Element) o2;
          int result = e1.j == e2.j ? 0: e1.j - e2.j;
          System.out.println("Comparator.compare(" + o1 + ", " + o2 + "): " + result);
          return result;
    }You will find 3 sets: a HashSet, a TreeSet, which uses the comparable nature of Element, and another TreeSet, which avails itself of a comparator in order to sort its elements.
    What I would like to prove by this is the following:
    HashSet makes use of hashCode() and where necessary of equals(Object o) to make store and to check upon the equality of object.
    TreeSet with Comparable does not make use of hashCode() and equals(Object o), but only compareTo(Object o) to store object and and check for their equality.
    TreeSet with Comparator does not make use of hashCode() and equals(Object o), bu only compare(Object o1, Object o2) to store object and and check for their equality.
    The output of the program demonstrates the last 3 statements.
    Now to go back to what the Comparator specs says, if you add two object such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) you will notice that the second element is indeed added to the Set. I have made sure that hashCode() has been redefined to be consistent with equals(Object o) and that compareTo(Object o) and compare(Object o1, Object o2) to be inconsistent with equals.
    Now I am eager to hear from you, all the more since tommorrow I am sitting the scjp 1.4.
    thanks for your help in advance
    luca iacono

    I wouldn't call it an issue of the contract not being fulfilled. The slight inaccuracy ("will not" instead o "may not") occurs in an example of what can go wrong if the user of the class doesn't fulfill his side of the contract. That wording is correct for the current implementation of HashSet, but not for all cases.
    The quote you gave was just an example of this statement: "If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave 'strangely.' In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals."
    Basically this statement says, "If your Comparator is not consistent with equals, don't expect the sorted set or sorted map to behave like it should." I'm not going to concern myself too much with the fact that it may not always behave badly if I break the rules.

  • How to Enable Compare.api in Acrobat X

    I have purchased and installed Acrobat X ver 10.1.1 and would like to use the compare feature.  When I select the View dropdown menu where this feature should be listed I do not see it there.  I then went to Help -> About Adobe Plugins and saw that the Compare.api plugin is not installed.  I inserted my CD to reinstall and do not see this as an option to install from the CD.  Can somebody please let me know how I can get this plugin installed?  I've looked on the Adobe website without finding any answer.
    Thaks in advance for your help!

    Do you use Acrobat X Pro or Acrobat X Suite?

  • IMac Tech Specs vs. PC's

    I'm a PC user thinking of buying an iMac. Does iMac require less memory and hard drive space than PC? The standard specs. for an iMac is 1GB memory and 250 GB HD. If it were a PC I would consider 2GB/320GB the standard (for me anyway). I realize that I can upgrade on an iMac, but is it necessary? Can I compare the specs of an iMac with that of a PC literally, or do they have very different requirements?

    Bus speeds, *NOW*, all the same...
    not the little yellow ones...
    {quote:title=corelle wrote:}but is it necessary?{quote}
    it all come down to *$$$*...
    With all memory requirements being the same, Apple handpicks and configures all the components to run with absolute optimization on Apple’s own OS X operating system. There are virtually no bottlenecks or hardware components that aren’t taken advantage of to their utmost. What this means is with two similarly configured Mac and PC computers, the Mac will outperform the PC.
    This is also true even when comparing Windows verses Windows on Mac vs PC similar configurations.
    see charts here:
    http://www.barefeats.com/macvpc.html
    as for costs:
    http://www.switchingtomac.com/wp/mac-vs-pc-part-5-hardware/
    It's all up on the actual hardware inside a Mac, Apple uses leading technology in their computers, in many cases going above industry standard and on top of this they have the added advantage of being able to leveraging their own OS to run at peek optimization and performance...
    Thanks.
    den
    .

  • Named Parameters in Queries - the spec vs the Java EE 5 Tutorial

    Hi,
    I've been studying the topic about the named parameters in JPA and found one inconsistency between the spec and the Java EE 5 Tutorial. Could anyone lend me a helping hand understanding it?
    The Java Persistence API spec (ejb-3_0-fr-spec-persistence.pdf) 3.6.3 "Named Parameters" at page 69 reads:
    The use of named parameters applies to the Java Persistence query language, and is not defined for native queries.
    whereas the Java EE 5 Tutorial in the section Named Parameters in Queries (http://java.sun.com/javaee/5/docs/tutorial/doc/PersistenceIntro3.html#wp81563) of The EntityManager reads:
    Named parameters are case-sensitive, and may be used by both dynamic and static queries.
    I think that the reality is that every JPA provider supports named parameters in dynamic and static queries, but it's not fully supported by the spec (yet not forbidden).
    Jacek
    Jacek Laskowski
    http://www.jaceklaskowski.pl

    Hi Jacek,
    These are not conflicting statements. Named parameters apply to the Java Persistence
    Query language only, not to native queries. There are two ways to specifiy
    Java Persistence Query Language queries : dynamically and statically. You can
    either pre-define the query string using @NamedQuery
    static :
    @NamedQuery(name="findPersonByName",
    query="SELECT OBJECT(p) FROM Person p WHERE p.name LIKE :pName")
    OR, pass the query string in at runtime using EntityManger.createQuery
    dynamic :
    em.createQuery("SELECT OBJECT(p) FROM Person p WHERE p.name LIKE :pName")
    The dynamic approach allows you to construct portions of the query string at runtime.
    My example doesn't do that but the point is that use of the createQuery()
    API is considered the dynamic approach.
    The crux of the Java EE 5 Tutorial statement you referenced is that named parameters apply to
    both the static and dynamic use of Java Persistence Query Language queries.
    --ken                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Acrobat Visual Compare Bug

    Dear all,
    We have created visual compare API using Adobe Acrobat sdk 8.
    On that we have found one issue in the output report as follows
    When i compare two pdf files (compare A and compare B), pages of document has been duplicated in the output report without text labeling as document B. We have marked those pages as extra pages in the inserted screenshot.
    Can anyone please help me on this.
    Regards,
    Jayakrishnan

    Hi mnav2015,
    According to your description, when you add a new parameter in dataset query, the parameter doesn’t generate automatically in report parameters.
    In Reporting Service, when we define a dataset query that contains a query variable, the query command is parsed. For each query variable, a corresponding dataset parameter and report parameter are created automatically. As we tested in Visual Studio 2008,
    the query designer contains the query, after refreshing the fields, the corresponding report parameter will display automatically.
    So in your scenario, I would like to know how about your query with the parameter. If possible, please provide some screenshots of results before and after you add a new parameter in dataset query.
    Reference:
    How to: Associate a Query Parameter with a Report Parameter
    If you have any question, please feel free to ask.
    Best regards,
    Qiuyun Yu
    Qiuyun Yu
    TechNet Community Support

  • Where can find java API to dwl

    does anyone know if i can download java API spec from sun.. since i cant find where I can dwl and i wish to use it offline....

    dwl?
    sure - its on the downloads page.

  • Invoking Acrobat PDF compare from Java code

    Need to invoke the Adobe Acrobat XI's PDF compare feature using Java code.
    Description:
    ========
    We are exploring the possibilities of passing 2 PDF's from Java to Adobe Acrobat XI SDK to get the compared results in new PDF.
    If anyone has explored with this feature... please share your thoughts.

    You would need to write a custom plugin to Acrobat that calls the compare APIs (as they are only accessible from C/C++).  Your plugin can then expose whatever from of IAC/IPC that it wishes to communicate with your Java code.
    Of course, this is all DESKTOP code since Acrobat can't be used on a server.

  • Show and Share API : Uploading a video to a particular user group

    Hi,
    I am trying to find a API spec for uplaoading a vide in Show and Share portal for a specific user group. But could not find such thing as usergroup, but just the Content Viewership is available where we need to input all the ____USERNAME____ to upload with access to the users rather than a user group.
    I would like to know if anyone can direct me to upload to specific usergroup rather than user.
    Thanks in Advnace,
    JK

    Jeffrey,
    What is the content format of the ORIGINAL source comment?
    - download MEDIAINFO video analysis program from the web.
      http://mediainfo.sourceforge.net/en
    - After installed, file->open-> pick your source file with issue.
    - change the view to TEXT and copy\paste the results into a
      TEXT file and upload here.
    From the MXE, highlight the SnS-MXE job and right click and
    open the JOB XML.  Same thing save the XML code in a text
    file and upload here
    Sidenote:  did you download the DMSprofile.zip file from the web
    and install the DMS profiles in the profile directory?
    Thanks
    T.

  • Java API for MDM Catalog-2

    Any idea folks where I can get the API specs from MDM Cat-2.
    Thanks.
    Anshuman.

    Hello Anshuman,
                            I have found some guides on MDM Catalog 2.0 but to download you need to have Marketplace account here is d link
    <a href="http://https://websmp203.sap-ag.de/~form/sapnet?_SHORTKEY=01100035870000679533&">MDM Catalog guides</a>
    Regards
    Tejas..................

  • API for MIDP 2.0

    hi guys!
    where can i download the MIDP2.0 API? i cant find it on google....
    Thanks

    Hi,
    You can download the MIDP2.0 API spec from the following link:
    http://jcp.org/aboutJava/communityprocess/final/jsr118/index.html
    When it's your first time to use MIDP, please take a look at the J2ME section within this java.sun.com site to read some basic tutorials and for downloading the Sun Wireless Toolkit (the emulator that you can use for testing your mobile application (i.e. MIDlet).
    Happy MIDP coding!
    Jasper

  • Ipad 4th gen spec check

    how do we check specs of the ipad eg like a system resources etc in win.
    battery doc app is showing my processor as A5X in my ipad4th gen. It has a lightning connector.

    Check the model number on the back.
    Compare iPad Specs http://www.apple.com/ipad/compare/
    Identifying iPad models
    http://support.apple.com/kb/HT5452?viewlocale=en_US&locale=en_US
     Cheers, Tom

  • GregorianCalendar before/after

    There are two methods called before and after in the GregorianCalendar class that don't appear to work as expected. You would think myObject.before(myOtherObject) would return true if the moment in time represented by myObject was before the moment in myOtherObject. Indeed, this is how the two methods are used in countless examples on the web and in a book...
    However, according to the API spec, this method only compares the TIME portion after converting to UTC... I confimed this with a code sample. This makes absolutely no sense to me and I can't believe the Java developers would do this. What am I missing?
    Thanks!

    It compares the time field (not portion)
    if you go to the api docs "Field Summary" and click on time, you'll get this
    time
    protected long timeThe currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.

  • The difference between regular ram and "apple ram" from 3rd parties

    So I made a previous post on the Macrumors forums regarding whether the RAM issues in the Unibody MacBook have been resolved or not: http://forums.macrumors.com/showthread.php?t=692653
    It seems that most people who bought OCZ ram have had no issues what so ever. I am located in Canada so a store like CanadaComputers is great for me because I can go pick it up instead of having it delivered and pay shipping costs (as opposed to Newegg.ca). On their website, they have two modules with similar specs expect one is found in the Apple ram section and the other is in the regular ram.
    Apple Ram (OCZ)
    http://canadacomputers.com/index.php...80&cid=RAM.188
    Regular Ram (OCZ)
    http://canadacomputers.com/index.php...id=RAM.187.778
    When comparing the specs, the only difference I can see is that the CL on the Apple ram is 7-7-7-20. The CL on the regular ram is CL 8-8-8-27.
    The price difference between the two is $57! OCZ's website confirms that one is made specifically for Apple Notebooks and the other is just regular.
    Can anyone explain what CL is and whether it is something that needs to be taken into consideration. Thanks.

    That only needs to be taken into consideration if you're using applications which are very demanding about performance, which aren't intended for usage by home users. Click here for more information.
    (43042)

  • HSDPA 10.2 in Nokia N8 isn't working?

    When I compare the speed of internet browsing thru packect data between Nokia N8 & HTC Wildfire ...there's a lot to write about it..
    Tested thru the same operator/carrier sim for both the mobiles..with normal 2G connection
    For HTC the speed is around 200KBPS where as for Nokia N8 its damn...20 KBPS max..
    HTC shows H symbol where as N8 shows E symbol. Tried changing the connection mode changed to Dual Mode and GSM by default.
    No idea on whats happening or isthere something really missingout in N8 or setup required.
    Mobiles compared are Nokia N8 and HTC Wildfire
    Operator is Airtel Andhra Pradesh
    Sim is 64 K
    Tested at the same location (with the same tower coverage)
    Tried rebooting by deleting all the cache too
    When I compare the specs for both mobiles only the difference is HTC is 512 RAM where as N8 is 256 RAM. All other features are N8 is better than HTC.
    Any suggestoins from forum veteran members or NOKIA should really look into it?
    Solved!
    Go to Solution.

    Sorry professior's and other mates....for the confusion that's been going on....
    Just to brake somewhere, I have break'd the HTC to get the SIM out. The moment I inserts that SIM in my Nokia N8 it started showing 3.5G, ofcousre after restart only.
    So, my observations are,
    though the the SIM in HTC said to be 2G only, somehow, 3G got activated on that and that is the reason it's showing 3.5G in N8 because network mode is set to 'Dual' in N8. And when I tried to download a game from store of 12GB size, I could get it in minute with a amazing constant speed of 264kBps.
    And when I insert the SIM of N8 which is said to be 2G only in HTC, the story is same, max speed is around 20 kBps.
    So, finally what we understood is, though the operator is charging for 2G rentals, infact the SIM is 64K(no confusion over here, 64K is written on physical SIM and that's how they differentiate it here in our location), it's actually functioning as 3G...this is allabout SIM of HTC. Guess, Dual mode is been selected even in HTC too...Whereas, the same case with the SIM of N8, but it's working perfectly fine what it is said to be, that is 2G.
    So conclusion is, my I'm proud to be owner of my Nokia N8, the best symbian phone that I ever used since a decade.
    Thanks dudes for all your time and suggestions.
    Note: Now, I can not go back to the network operator and complaint on my colleagues HTC, though both of us are paying same charges but not getting the same kind of speed. If I do that, my colleague will never forgive me. Let him enjoy the blazzing 3G speed for normal 2G charges.
    So, thanks for all for feeding apples, oranges.
    Thanks & Regards,
    Karunakar Konne,
    Moderator's note: Website removed from signature as we don't allow any form of publicity on the Discussion Boards.

Maybe you are looking for