Results 1 to 2 of 2

Thread: HashSet anomaly

  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default HashSet anomaly

    I've tried running this program against a text file and it seems to take forever on my pc.

    To run this program from the command line, use:

    C:\Users\Administrator\Documents\Java Source Code>java SetTest Alice.txt

    Alice.txt is a text file that can be downloaded from Alice.txt - Windows Live or Main Page - Gutenberg.

    Any ideas why?

    :confused:

    Java Code:
     import java.util.*;
    
     /**
        This program uses a set to print all unique words in
        System.in.
     */
     public class SetTest
     {
        public static void main(String[] args)
        {
           Set<String> words = new HashSet<String>(); // HashSet implements Set
           long totalTime = 0;
    
           Scanner in = new Scanner(System.in);
           while (in.hasNext())
           {
              String word = in.next();
              long callTime = System.currentTimeMillis();
              words.add(word);
              callTime = System.currentTimeMillis() - callTime;
              totalTime += callTime;
           }
    
           Iterator<String> iter = words.iterator();
           for (int i = 1; i <= 20; i++)
              System.out.println(iter.next());
           System.out.println(". . .");
           System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds.");
        }
     }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

Similar Threads

  1. heap memory usage anomaly?
    By jon80 in forum Threads and Synchronization
    Replies: 1
    Last Post: 06-30-2009, 07:56 AM
  2. Replies: 0
    Last Post: 10-15-2008, 06:45 PM
  3. HashSet and AbstractSet both Implements Set
    By rkjsohi2 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 12:04 PM
  4. Working with HashSet and TreeSet
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:34 PM
  5. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:34 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
  •