Problem while sorting an ArrayList

Discussion in 'Java' started by ruds, Oct 7, 2008.

  1. ruds

    ruds Guest

    Hi,
    I'm having problem whjile sorting arrayList.
    Actually I'm geting values from a Hashtable and adding them to the
    ArrayList.
    when I do Collections.sort(mylist);
    I do not go further in my program, as it comes out at this point.
    My ArrayList contains abt 4000-5000 values.
    I even tried myList.toArray(arr); and then sorting the array, but even
    this is not working.
    Can some one tell me wht must be the problem?

    Thanks,
    ruds, Oct 7, 2008
    #1
    1. Advertising

  2. ruds wrote:
    > Hi,
    > I'm having problem whjile sorting arrayList.
    > Actually I'm geting values from a Hashtable and adding them to the
    > ArrayList.
    > when I do Collections.sort(mylist);
    > I do not go further in my program, as it comes out at this point.


    I'm guessing that "comes out" means either "hangs" or "throws an
    exception". In either case, I'd guess that the "compareTo" and
    "equals" methods of the objects in the ArrayList are faulty. If they
    don't obey the rules for those methods, Collection.sort() can cause
    those kinds of problems.
    Mike Schilling, Oct 7, 2008
    #2
    1. Advertising

  3. On Oct 7, 8:43 am, ruds <> wrote:
    > Hi,
    > I'm having problem whjile sorting arrayList.
    > Actually I'm geting values from a Hashtable and adding them to the
    > ArrayList.
    > when I do Collections.sort(mylist);
    > I do not go further in my program, as it comes out at this point.
    > My ArrayList contains abt 4000-5000 values.
    > I even tried myList.toArray(arr); and then sorting the array, but even
    > this is not working.
    > Can some one tell me wht must be the problem?
    >
    > Thanks,


    Have you read and understood the javadocs for the sort method? It
    won't sort unless you satisfy your side of the contract.
    softwarepearls_com, Oct 7, 2008
    #3
  4. ruds

    ruds Guest

    > Have you read and understood the javadocs for the sort method? It
    > won't sort unless you satisfy your side of the contract.


    Can u explain in detail?
    My code is:
    limit=eid.size();
    for(i=0;i<limit;i++)
    {
    str=(String)eid.get(i);
    stress.add(eid_stress.get(str));
    }
    Collections.sort(stress);

    So where am I going wrong?
    ruds, Oct 7, 2008
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    ruds schreef:
    >> Have you read and understood the javadocs for the sort method? It
    >> won't sort unless you satisfy your side of the contract.

    >
    > Can u explain in detail?
    > My code is:
    > limit=eid.size();
    > for(i=0;i<limit;i++)
    > {
    > str=(String)eid.get(i);
    > stress.add(eid_stress.get(str));
    > }
    > Collections.sort(stress);
    >
    > So where am I going wrong?


    By not giving us enough code. What is eid_stress (have a look at Java
    naming conventions, btw), what is stress, what is eid, what does
    eid_stress.get return, is it comparable?

    Oh and there is no need at all to make your code complicated by using
    the variable you call ‘limit’. A proper jvm will optimize this away.

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iEYEARECAAYFAkjrLOwACgkQBGFP0CTku6O7vQCghAnCq8gk7eu9sHzJsX22AUF1
    pGcAoLiCSMuuk6CvHhqyEjFQOxDBJWN6
    =LDPu
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Oct 7, 2008
    #5
  6. ruds

    Evans Guest

    On 7 Oct, 09:59, ruds <> wrote:
    > > Have you read and understood the javadocs for the sort method? It
    > > won't sort unless you satisfy your side of the contract.

    >
    > Can u explain in detail?
    > My code is:
    > limit=eid.size();
    > for(i=0;i<limit;i++)
    > {
    >  str=(String)eid.get(i);
    >  stress.add(eid_stress.get(str));}
    >
    > Collections.sort(stress);
    >
    > So where am I going wrong?


    Its hard to know what 'its not working' means in this case. Could you
    tell us the error message or the exception that is thrown? For a
    start, Collections.sort() method takes a list as parameter, and that
    list will be sorted according to the natural ordering of its elements.
    That means that 'Hello' and 'hello' are not going to be next to each
    other.

    Just post a small runnable code so we can where you're going wrong.

    --
    Evans
    http://www.jroller.com/evans
    Evans, Oct 7, 2008
    #6
  7. package snippets.demo;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    /**
    * @author Stefan Rybacki
    */
    public class CollectionSort {
    private static final int MAX_ENTRIES = 50000;
    private static final char[] characters =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    .toCharArray();

    /**
    * @param maxLength
    * maximum length of the String generated
    * @return a randomly generated String
    */
    private static String randomString(int maxLength) {
    int length = (int) (Math.random() * maxLength);

    StringBuffer buffer = new StringBuffer(length);

    for (int i = 0; i < length; i++) {
    int c = (int) (Math.random() * characters.length);
    buffer.append(characters[c]);
    }

    return buffer.toString();
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    // create list of strings
    List<String> listToSort = new ArrayList<String>(MAX_ENTRIES);

    // fill list of strings
    for (int i = 0; i < MAX_ENTRIES; i++) {
    listToSort.add(randomString(100));
    }

    // now sort list
    Collections.sort(listToSort);

    // print sorted list
    for (String s : listToSort) {
    System.out.println(s);
    }
    }

    }
    Stefan Rybacki, Oct 7, 2008
    #7
  8. ruds

    ruds Guest

    > Its hard to know what 'its not working' means in this case. Could you
    > tell us the error message or the exception that is thrown?  For a
    > start, Collections.sort() method takes a list as parameter, and that
    > list will be sorted according to the natural ordering of its elements.
    > That means that 'Hello' and 'hello' are not going to be next to each
    > other.
    >
    > Just post a small runnable code so we can where you're going wrong.
    >
    > --
    > Evanshttp://www.jroller.com/evans


    My collections:
    ArrayList eid=new ArrayList();
    ArrayList stress=new ArrayList();
    Hashtable eid_stress=new Hashtable();

    I have a HashTable eid_stress which store keys with corresponding
    stress as values
    What I want to do is based on certain criteria I get the elements
    store them in eid arraylist, get their corresponding stresses and
    store in stress arraylist. Sort the stressess and get the highes one
    and its element.
    So as given in code above that is exactly what I'm doing, but my
    program just terminates as soon as it encounters
    Collections.sort(stress);
    line.

    Why is this happening?
    ruds, Oct 7, 2008
    #8
  9. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    ruds schreef:
    >> Its hard to know what 'its not working' means in this case. Could you
    >> tell us the error message or the exception that is thrown? For a
    >> start, Collections.sort() method takes a list as parameter, and that
    >> list will be sorted according to the natural ordering of its elements.
    >> That means that 'Hello' and 'hello' are not going to be next to each
    >> other.
    >>
    >> Just post a small runnable code so we can where you're going wrong.
    >>
    >> --
    >> Evanshttp://www.jroller.com/evans

    >
    > My collections:
    > ArrayList eid=new ArrayList();
    > ArrayList stress=new ArrayList();
    > Hashtable eid_stress=new Hashtable();
    >
    > I have a HashTable eid_stress which store keys with corresponding
    > stress as values
    > What I want to do is based on certain criteria I get the elements
    > store them in eid arraylist, get their corresponding stresses and
    > store in stress arraylist. Sort the stressess and get the highes one
    > and its element.
    > So as given in code above that is exactly what I'm doing, but my
    > program just terminates as soon as it encounters
    > Collections.sort(stress);
    > line.


    Ok, I should have been clearer before: read
    http://mindprod.com/jgloss/sscce.html and the link below in my
    signature, then come again.

    Consider using generics. No, better: I won’t help you if you don’t (but
    feel free to ask help if they trouble you).

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iEYEARECAAYFAkjrQssACgkQBGFP0CTku6NX6ACgyd7STMUttuawhdB6v9HFXtNV
    f1oAoNbz+5oHqGf3pbMVfKLrVR3GXyi0
    =LAWs
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Oct 7, 2008
    #9
  10. ruds

    Daniel Pitts Guest

    ruds wrote:
    >> Have you read and understood the javadocs for the sort method? It
    >> won't sort unless you satisfy your side of the contract.

    >
    > Can u explain in detail?
    > My code is:
    > limit=eid.size();
    > for(i=0;i<limit;i++)
    > {
    > str=(String)eid.get(i);
    > stress.add(eid_stress.get(str));
    > }
    > Collections.sort(stress);
    >
    > So where am I going wrong?

    That's all well and good, but what is the class of object in 'stress'?
    That will help us figure out what's going on.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Oct 7, 2008
    #10
  11. ruds

    Roedy Green Guest

    On Mon, 6 Oct 2008 23:43:33 -0700 (PDT), ruds <>
    wrote, quoted or indirectly quoted someone who said :

    >Hi,
    >I'm having problem whjile sorting arrayList.
    >Actually I'm geting values from a Hashtable and adding them to the
    >ArrayList.
    >when I do Collections.sort(mylist);
    >I do not go further in my program, as it comes out at this point.
    >My ArrayList contains abt 4000-5000 values.
    >I even tried myList.toArray(arr); and then sorting the array, but even
    >this is not working.
    >Can some one tell me wht must be the problem?


    That is like phoning up and saying "Doctor, I am feeling sick. What is
    the matter with me?".

    See http://mindprod.com/jgloss/sort.html
    for some sample code that does work. Compare your with it. Or else
    post code.

    see http://mindprod.com/jgloss/sscce.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com/politics/harper.html
    Anyone but Harper for Prime Minister of Canada
    Roedy Green, Oct 8, 2008
    #11
  12. ruds

    ruds Guest

    My code:
    class Summary
    {
    ArrayList eid=new ArrayList(); //contains String objects (employee id)
    ArrayList stress=new ArrayList(); // contains String objects(stress of
    each id)
    Hashtable eid_stress=new Hashtable(); // HashTable mapping eid to
    stress

    public static void main(String args[])
    {
    int limit=eid.size();
    for(i=0;i<limit;i++)
    {
    str=(String)eid.get(i);
    stress.add(eid_stress.get(str));
    }
    Collections.sort(stress);
    System.out.println("Max stress="+stress.get(limit-1));
    }

    The Program doesnot throw any exception or any other message.
    It compiles properly and also does not give any runtime exception too.
    after the execution starts the program just terminates when it reaches
    "Collections.sort(stress);" line.
    I even tried converting the arraylist to array and then sorting the
    array but with no luck. :(

    Ruds
    ruds, Oct 8, 2008
    #12
  13. ruds wrote:
    > My code:
    > class Summary
    > {
    > ArrayList eid=new ArrayList(); //contains String objects (employee id)
    > ArrayList stress=new ArrayList(); // contains String objects(stress of
    > each id)
    > Hashtable eid_stress=new Hashtable(); // HashTable mapping eid to
    > stress
    >
    > public static void main(String args[])
    > {
    > int limit=eid.size();
    > for(i=0;i<limit;i++)
    > {
    > str=(String)eid.get(i);
    > stress.add(eid_stress.get(str));
    > }
    > Collections.sort(stress);
    > System.out.println("Max stress="+stress.get(limit-1));
    > }
    >
    > The Program doesnot throw any exception or any other message.
    > It compiles properly and also does not give any runtime exception too.
    > after the execution starts the program just terminates when it reaches
    > "Collections.sort(stress);" line.
    > I even tried converting the arraylist to array and then sorting the
    > array but with no luck. :(


    As posted, the program does not compile. With fixes to the compilation
    problems it fails, as one would expect, with an
    ArrayIndexOutOfBoundsException on the stress.get(limit-1) call.

    I suggest working on a usable example. Compile and run it to make sure
    it does compile, and does reproduce the problem. The effort is well
    worth while. Often, the act of trying to produce a short program that
    reproduces a problem can isolate it and make it obvious.

    Reproducing the actual content in the ArrayList is critical. If the
    failure really is in Collections.sort, it is probably due to
    inconsistent comparisons among the elements.

    Patricia
    Patricia Shanahan, Oct 8, 2008
    #13
  14. ruds

    ruds Guest

    On Oct 8, 9:55 am, Patricia Shanahan <> wrote:
    > ruds wrote:
    > > My code:
    > > class Summary
    > > {
    > > ArrayList eid=new ArrayList(); //contains String objects (employee id)
    > > ArrayList stress=new ArrayList(); // contains String objects(stress of
    > > each id)
    > > Hashtable eid_stress=new Hashtable(); // HashTable mapping eid to
    > > stress

    >
    > > public static void main(String args[])
    > > {
    > >  int limit=eid.size();
    > >  for(i=0;i<limit;i++)
    > >  {
    > >   str=(String)eid.get(i);
    > >   stress.add(eid_stress.get(str));
    > > }
    > >  Collections.sort(stress);
    > > System.out.println("Max stress="+stress.get(limit-1));
    > > }

    >
    > > The Program doesnot throw any exception or any other message.
    > > It compiles properly and also does not give any runtime exception too.
    > > after the execution starts the program just terminates when it reaches
    > > "Collections.sort(stress);" line.
    > > I even tried converting the arraylist to array and then sorting the
    > > array but with no luck. :(

    >
    > As posted, the program does not compile. With fixes to the compilation
    > problems it fails, as one would expect, with an
    > ArrayIndexOutOfBoundsException on the stress.get(limit-1) call.
    >
    > I suggest working on a usable example. Compile and run it to make sure
    > it does compile, and does reproduce the problem. The effort is well
    > worth while. Often, the act of trying to produce a short program that
    > reproduces a problem can isolate it and make it obvious.
    >
    > Reproducing the actual content in the ArrayList is critical. If the
    > failure really is in Collections.sort, it is probably due to
    > inconsistent comparisons among the elements.
    >
    > Patricia- Hide quoted text -
    >
    > - Show quoted text -


    Actually, the program that I want is already working fine. Now I want
    to use a different logic for same program to reduce the total
    execution time.
    So this is working fine in my previous porgram with the same I/p's but
    I dont know what has happened now..
    ruds, Oct 8, 2008
    #14
  15. ruds

    Mark Space Guest

    ruds wrote:

    > The Program doesnot throw any exception or any other message.
    > It compiles properly and also does not give any runtime exception too.



    No, it does not compile. Try again.



    Compiling 1 source file to C:\Users\Dev\misc\ArraySort\build\classes
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:34: non-static
    variable eid cannot be referenced from a static context
    int limit = eid.size();
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:35: cannot find symbol
    symbol : variable i
    location: class arraysort.Summary
    for( i = 0; i < limit; i++ ) {
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:35: cannot find symbol
    symbol : variable i
    location: class arraysort.Summary
    for( i = 0; i < limit; i++ ) {
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:35: cannot find symbol
    symbol : variable i
    location: class arraysort.Summary
    for( i = 0; i < limit; i++ ) {
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:36: cannot find symbol
    symbol : variable str
    location: class arraysort.Summary
    str = (String) eid.get( i );
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:36: cannot find symbol
    symbol : variable i
    location: class arraysort.Summary
    str = (String) eid.get( i );
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:36: non-static
    variable eid cannot be referenced from a static context
    str = (String) eid.get( i );
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:37: cannot find symbol
    symbol : variable str
    location: class arraysort.Summary
    stress.add( eid_stress.get( str ) );
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:37: non-static
    variable eid_stress cannot be referenced from a static context
    stress.add( eid_stress.get( str ) );
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:37: non-static
    variable stress cannot be referenced from a static context
    stress.add( eid_stress.get( str ) );
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:39: non-static
    variable stress cannot be referenced from a static context
    Collections.sort( stress );
    C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java:40: non-static
    variable stress cannot be referenced from a static context
    System.out.println( "Max stress=" + stress.get( limit - 1 ) );
    Note: C:\Users\Dev\misc\ArraySort\src\arraysort\Main.java uses unchecked
    or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    12 errors
    BUILD FAILED (total time: 0 seconds)
    Mark Space, Oct 8, 2008
    #15
  16. ruds

    Evans Guest

    On 8 Oct, 05:37, ruds <> wrote:
    > My code:
    > class Summary
    > {
    > ArrayList eid=new ArrayList(); //contains String objects (employee id)
    > ArrayList stress=new ArrayList(); // contains String objects(stress of
    > each id)
    > Hashtable eid_stress=new Hashtable(); // HashTable mapping eid to
    > stress
    >
    > public static void main(String args[])
    > {
    >  int limit=eid.size();
    >  for(i=0;i<limit;i++)
    >  {
    >   str=(String)eid.get(i);
    >   stress.add(eid_stress.get(str));}
    >
    >  Collections.sort(stress);
    > System.out.println("Max stress="+stress.get(limit-1));
    >
    > }
    >
    > The Program doesnot throw any exception or any other message.
    > It compiles properly and also does not give any runtime exception too.
    > after the execution starts the program just terminates when it reaches
    > "Collections.sort(stress);" line.
    > I even tried converting the arraylist to array and then sorting the
    > array but with no luck. :(
    >
    > Ruds


    Do you enjoy wasting people's time, Ruds?
    Why don't you go a head and post the code that is giving you hard
    time?

    --
    Evans
    http://www.jroller.com/evans
    Evans, Oct 12, 2008
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,727
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,334
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,344
  4. Philipp
    Replies:
    6
    Views:
    902
    Arne Vajhøj
    May 28, 2008
  5. Holysmoke

    Problem with Sorting of DataGrid with Arraylist binding

    Holysmoke, Oct 20, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    167
    Scott Mitchell [MVP]
    Oct 20, 2004
Loading...

Share This Page