Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Comparator)

  1. #1
    aanglade is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Comparator)

    Hi,

    I am currently migrating application from java 1.4 to 1.6.
    After the migration, I have checked that the return values are sorted differently. This changes of behavior seems to come from changes in the java class java.util.collection.

    To have the application works correctly(like before), I have to add the following lines in bold (returning -1):

    implements Comparator
    {
    public int compare(Object a, Object b)
    {
    Short posA = (Short)((ArrayList)a).get(0);
    Short posB = (Short)((ArrayList)b).get(0);


    if (posA.intValue() > posB.intValue()) {
    return 1;
    }


    if (posA.intValue() < posB.intValue())
    {
    return -1;
    }



    return 0;
    }
    }

    As I have thousand of applications and comparators that may have the same problem.
    can you propose a better idea than doing this modification in all the applications and comparators ?

    Thanks !

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Compara

    Can you post an SSCCE that demonstrates what you're talking about?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Compara

    Are you saying the original code returned either a 1 or a 0? Never a negative number?

    Would this expression return the correct value:
    Java Code:
     return (posA.intValue() - posB.intValue());
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Compara

    I am sure that the designers of the API would not have rendered it incompatible with earlier versions. That was the whole point of erasure with Generics and why you should only get warnings, not errors, when recompiling raw classes. As stated earlier, an example of what used to work and what now doesn't would be helpful.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    aanglade is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Re: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Compara

    Quote Originally Posted by jim829 View Post
    I am sure that the designers of the API would not have rendered it incompatible with earlier versions. That was the whole point of erasure with Generics and why you should only get warnings, not errors, when recompiling raw classes. As stated earlier, an example of what used to work and what now doesn't would be helpful.

    Regards,
    Jim
    Thanks for your answers. I will check with developers if I can get a SSCE to better explain our concern related to migration.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Compara

    Your devs were utilising a bug in the comparison code.
    I vaguely remember this coming up around the time of 1.5.

    From the 1.4.2 api docs for the Comparator.compare() method (downloadable from the Oracle archives):
    "
    Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
    "

    Just to check, the 1.3.1 original release docs were identical.

    So, put simply, your devs were not following the specification for compare(). That it worked at the time is really irrelevant.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Compara

    Quote Originally Posted by Tolls View Post
    So, put simply, your devs were not following the specification for compare(). That it worked at the time is really irrelevant.
    An often overlooked possibility that is more often true than one would think: your code is bugged but you just don't see it yet until you upgrade to a version of Java which exposes the bug
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Migration from Java 1.4 to Java1.6(changes in java.util.collection impact Compara

    I'm sure I remember several threads on the old Sun board when the fix happened.
    Or I could be merging together any number of fixes that exposed people misusing some interface or other...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. java.util.Comparator Implementation
    By Navatha in forum New To Java
    Replies: 6
    Last Post: 11-05-2010, 02:48 AM
  2. Java - Comparator, sorting arrayLists
    By Kerrai in forum New To Java
    Replies: 15
    Last Post: 10-24-2010, 10:41 PM
  3. Obsolete Collection, import java.util.Hashtable
    By newbiejava in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 05:36 PM
  4. implements java.util.Comparator<type>
    By finn in forum New To Java
    Replies: 2
    Last Post: 05-14-2010, 07:58 PM
  5. Java Object Migration
    By vijat in forum Advanced Java
    Replies: 1
    Last Post: 10-23-2009, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •