1 Reply Latest reply: Dec 27, 2003 3:28 AM by 807582 RSS

    collection problems

    807582
      Hi i pdont know whether anyone can help me, i created an application that parses strings into a collection and then uses an Arraylist to sort the collection and then print out contents of the collection, everything works up until it's time to creat a list this throws up an error, the code is below could anyone help me

      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import java.util.*;

      public class dbCollection
      {
           Map map = new TreeMap();
           String line = "Whats up coz, hello my mate, Absolutely fabulous, Marvin Gaye, greg wait up, big muther trucker";
           StringTokenizer st;
           List keys;
           
           int hcode = 0;
           
           public dbCollection()
           {
                st = new StringTokenizer(line,",");
                while (st.hasMoreTokens())
                {
                     String word = st.nextToken();
                     hcode = word.hashCode();
                //     Object o = map.get(word);
                //     if (o == null)
                          map.put(word, new Integer(hcode));
                //     map.put(word,hcode);
                }

                keys = new ArrayList(map.keySet());
                Collection.sort(keys);
                
                //write result to printwriter
                PrintWriter out = new PrintWriter();
                Iterator iterator = key.iterator();
                
                while (iterator.hasNext())
                {
                     Object key = iterator.next();
                     out.println(key + "," + map.get(key));
                }
           }
      }
        • 1. Re: collection problems
          807582
          Hi i pdont know whether anyone can help me, i created
          an application that parses strings into a collection
          and then uses an Arraylist to sort the collection and
          then print out contents of the collection
          Couple of questions:

          1. If you are sticking the items into a collection that is already
          sorted then why go to the trouble of putting them into a list
          and sorting the already sorted list. Why not just iterate over
          the keys and be done with it?

          2. Why didn't you use code tags to make your code in this
          post easier to read?

          3. Why use collections at all if all you want is a sorted list
          of substrings?
          import java.util.Arrays;
          
          public class Test
          {
              public static void main(String[] args)
              {
                  String line = "Whats up coz, hello my mate, Absolutely fabulous, Marvin Gaye, greg wait up, big muther trucker";
                  String[] lines = line.split("[ ]*,[ ]*");
                  Arrays.sort(lines);
                  for (int i = 0; i < lines.length; ++i)
                      System.out.println(lines);
          }
          }

          This output is as follows (in my locale):
          Absolutely fabulous
          Marvin Gaye
          Whats up coz
          big muther trucker
          greg wait up
          hello my mate