Problem while sorting an ArrayList

R

ruds

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,
 
M

Mike Schilling

ruds 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.

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.
 
S

softwarepearls_com

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.
 
R

ruds

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?
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ruds schreef:
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-----
 
E

Evans

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.
 
S

Stefan Rybacki

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);
}
}

}
 
R

ruds

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.

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?
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ruds schreef:
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-----
 
D

Daniel Pitts

ruds said:
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.
 
R

Roedy Green

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
 
R

ruds

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
 
P

Patricia Shanahan

ruds said:
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
 
R

ruds

ruds said:
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..
 
M

Mark Space

ruds said:
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)
 
E

Evans

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top