11 Replies Latest reply on Aug 8, 2003 2:58 PM by 807545

    Sort A HashTable/Set

    807545
      Hi all. How to sort a hashtable or set? I also want to print out. What I did is:

      <code>

      Set set = map.entrySet();
      List setList = new ArrayList(set);
                
      Collections.sort(setList);
                
      for(Iterator it = setList.iterator(); it.hasNext();)
      pw.println(it.next());

      </code>

      But it gave me error why I tried to run it:
      Exception in thread "main" java.lang.ClassCastException
      at java.util.Arrays.mergeSort(Arrays.java:1152)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.mergeSort(Arrays.java:1163)
      at java.util.Arrays.sort(Arrays.java:1079)
      at java.util.Collections.sort(Collections.java:113)
      at Sequence.findAllPhrases(Sequence.java:99)
      at Main.main(Main.java:284)

      I put the above code at Sequence.java. Thanks for your help.
        • 1. Re: Sort A HashTable/Set
          807545
          I believe the entryset values do not implement Comparable.

          HTH
          Mike
          • 2. Re: Sort A HashTable/Set
            807545
            Why do you want to sort a Set?

            Mathematically, a set has no order - use a List if you want a sorted collection
            • 3. Re: Sort A HashTable/Set
              807545
              Yes, that is why I stored it into List. But then still gave me error. Thanks for your reply anyway.
              • 4. Re: Sort A HashTable/Set
                807545
                But i tot that in Java API, also got method sort(List list). :P
                • 5. Re: Sort A HashTable/Set
                  807545
                  Supply a comparator.

                  Kind regards,
                    Levi
                  • 6. Re: Sort A HashTable/Set
                    807545
                    I tried with Comparator, but still give me the same error. How? What's wrong?

                              List setList = new ArrayList(set);
                              
                              Collections.sort(setList, new Comparator() { 
                                   public int compare(Object o1, Object o2) {    
                                        Object v1 = ((Map.Entry)o1).getValue();
                                        Object v2 = ((Map.Entry)o2).getValue();
                                        return ((Comparable)v1).compareTo(v2);

                                   }
                              });
                              
                              for(Iterator it = setList.iterator(); it.hasNext();)
                                   pw.println(it.next());
                    • 7. Re: Sort A HashTable/Set
                      798906
                      Just use a SortedMap instead of a Map, that way the values will be held in ascending key order.
                      • 8. Re: Sort A HashTable/Set
                        807545
                        It works for me:
                        import java.util.*;
                        
                        public class Test {
                            public static void main (String[] parameters) {
                                Map map = new HashMap ();
                        
                                for (int i = 10; i > 0; i --) {
                                    map.put (new Integer (i), new Integer (i));
                                }
                        
                                List list = new ArrayList (map.entrySet ());
                                Collections.sort (list, new Comparator () {
                                    public int compare (Object entryA, Object entryB) {
                                        return ((Comparable) ((Map.Entry) entryA).getValue ()).compareTo (((Map.Entry) entryB).getValue ());
                                    }
                                });
                        
                                Iterator iterator = list.iterator ();
                        
                                while (iterator.hasNext ()) {
                                    System.out.println (iterator.next ());
                                }
                            }
                        }
                        • 9. Re: Sort A HashTable/Set
                          807545
                          Yeah, I think my code should be correct. It can compile, but when I run/execute, it gave me error like this:
                          Exception in thread "main" java.lang.ClassCastException
                          at Sequence$1.compare(Sequence.java:99)
                          at java.util.Arrays.mergeSort(Arrays.java:1278)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.mergeSort(Arrays.java:1289)
                          at java.util.Arrays.sort(Arrays.java:1219)
                          at java.util.Collections.sort(Collections.java:155)
                          at Sequence.findAllPhrases(Sequence.java:97)
                          at Main.main(Main.java:282)

                          Actually what's wrong with my java file? I find the error until blurrr :'(
                          Anyway, thanks for all your replies.
                          • 10. Re: Sort A HashTable/Set
                            807545
                            Just a guess, but it would seem like you're comparing 2 objects of different classes and the compare method blindly does a cast (which fails).
                            • 11. Re: Sort A HashTable/Set
                              807545
                              A TreeSet maintains order, if you're looking to do a simple ascending ordering.