Results 1 to 7 of 7
  1. #1
    rain528919 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default change arraylist to hashSet or TreeSet

    I'm suppose to change arraylist to hashSet or TreeSet and test the speed
    not much progress for me so far. can someone help?

    public static ArrayList<String> getWords(Scanner input) {
    // ignore all but alphabetic characters and apostrophes
    input.useDelimiter("[^a-zA-Z']");
    // read all words and sort
    ArrayList<String> words = new ArrayList<String>();
    while (input.hasNext()) {
    String next = input.next().toLowerCase();
    words.add(next);
    }
    Collections.sort(words);

    // add unique words to new list and return
    ArrayList<String> result = new ArrayList<String>();
    if (words.size() > 0) {
    result.add(words.get(0));
    for (int i = 1; i < words.size(); i++) {
    if (!words.get(i).equals(words.get(i - 1))) {
    result.add(words.get(i));
    }
    }
    }
    return result;
    }

    // pre : list1 and list2 are sorted and contain no duplicates
    // post: constructs and returns an ArrayList containing the words
    // in common betweeen list1 and list2
    public static ArrayList<String> getOverlap(ArrayList<String> list1,
    ArrayList<String> list2) {
    ArrayList<String> result = new ArrayList<String>();
    int index1 = 0;
    int index2 = 0;
    while (index1 < list1.size() && index2 < list2.size()) {
    int comparison = list1.get(index1).compareTo(list2.get(index2));
    if (comparison == 0) {
    result.add(list1.get(index1));
    index1++;
    index2++;
    } else if (comparison < 0) {
    index1++;
    } else { // comparison > 0
    index2++;
    }
    }
    return result;
    }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: change arraylist to hashSet or TreeSet

    I'm not knowledgeable on the subject, but using code tags will make your code easier to read for people. More info here: The Ubiquitous Newbie Tips
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: change arraylist to hashSet or TreeSet

    can someone help
    Sure, if you define the problem more clearly...does it compile? Are there exceptions? Do you know how to use Sets? Do you know how to profile code? When you say test the speed, test it for what (iteration, lookup, order)?

  4. #4
    rain528919 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: change arraylist to hashSet or TreeSet

    this is the original code, I'm suppose to compare the performance speed between arraylist and set for 2 files. the teacher is asking us to convert the arraylist to set(treeset or hashseet). I understand that set doesn't have index, so I need to use iterator for the getOverlap


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class Vocabulary3 {
        public static void main(String[] q) throws FileNotFoundException {
            Scanner console = new Scanner(System.in);
            giveIntro();
    
            System.out.print("file #1 name? ");
            Scanner input1 = new Scanner(new File(console.nextLine())); // moby.txt
            System.out.print("file #2 name? ");
            Scanner input2 = new Scanner(new File(console.nextLine())); // hamlet.txt
            System.out.println();
    
            double time1 = System.currentTimeMillis();
            ArrayList<String> list1 = getWords(input1);
            ArrayList<String> list2 = getWords(input2);
            double time2 = System.currentTimeMillis();
            System.out.println("File read time=" + (time2-time1)/1000 + " seconds");
            
            time1 = System.currentTimeMillis();
            ArrayList<String> overlap = getOverlap(list1, list2);
            reportResults(list1, list2, overlap);
            time2 = System.currentTimeMillis();
            System.out.println("Process time=" + (time2-time1) + " milliseconds");
        }
    
        // post: reads all words from the given Scanner, turning them to lowercase
        //       and returning a sorted list of the vocabulary of the file
        public static ArrayList<String> getWords(Scanner input) {
            // ignore all but alphabetic characters and apostrophes
            input.useDelimiter("[^a-zA-Z']");
            // read all words and sort
            ArrayList<String> words = new ArrayList<String>();
            while (input.hasNext()) {
                String next = input.next().toLowerCase();
                words.add(next);
            }
            Collections.sort(words);
    
            // add unique words to new list and return
            ArrayList<String> result = new ArrayList<String>();
            if (words.size() > 0) {
                result.add(words.get(0));
                for (int i = 1; i < words.size(); i++) {
                    if (!words.get(i).equals(words.get(i - 1))) {
                        result.add(words.get(i));
                    }
                }
            }
            return result;
        }
    
        // pre : list1 and list2 are sorted and contain no duplicates
        // post: constructs and returns an ArrayList containing the words 
        //       in common betweeen list1 and list2
        public static ArrayList<String> getOverlap(ArrayList<String> list1,
                                                   ArrayList<String> list2) {
            ArrayList<String> result = new ArrayList<String>();
            int index1 = 0;
            int index2 = 0;
            while (index1 < list1.size() && index2 < list2.size()) {
                int comparison = list1.get(index1).compareTo(list2.get(index2));
                if (comparison == 0) {
                    result.add(list1.get(index1));
                    index1++;
                    index2++;
                } else if (comparison < 0) {
                    index1++;
                } else {  // comparison > 0
                    index2++;
                }
            }
            return result;
        }
    
        // post: explains program to user
        public static void giveIntro() {
            System.out.println("This program compares the vocabulary of two");
            System.out.println("text files, reporting the number of words");
            System.out.println("in common and the percent of overlap.");
            System.out.println();
        }
    
        // pre : overlap contains the words in common between list1 and list2
        // post: reports statistics about two word lists and their overlap
        public static void reportResults(ArrayList<String> list1,
                                         ArrayList<String> list2,
                                         ArrayList<String> overlap) {
            System.out.println("file #1 words = " + list1.size());
            System.out.println("file #2 words = " + list2.size());
            System.out.println("common words  = " + overlap.size());
    
            double percent1 = 100.0 * overlap.size() / list1.size();
            double percent2 = 100.0 * overlap.size() / list2.size();
            System.out.println("% of file 1 in overlap = " + percent1);
            System.out.println("% of file 2 in overlap = " + percent2);
        }
    }
    Last edited by rain528919; 07-13-2012 at 04:46 AM.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: change arraylist to hashSet or TreeSet

    See post 3 - your chances of getting help greatly improve if you answer each of those questions asked, and if you use the [code][/code] tags

  6. #6
    rain528919 is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: change arraylist to hashSet or TreeSet

    this is the changes that i made for part of it.
    since TreeSet can stored elements in sorted order so i deleted Collections.sort(words);

    Java Code:
        
    
    public static Set<String> getWords(Scanner input) {
            // ignore all but alphabetic characters and apostrophes
            input.useDelimiter("[^a-zA-Z']");
            // read all words and sort
            
            Set<String> words = new TreeSet<String>();
            while (input.hasNext()) {
                String next = input.next().toLowerCase();
                words.add(next);
            }

    second part
    Java Code:
           
     Set<String> result = new HashSet<String>();
            if (words.size() > 0) {
                result.add(words.get(0));
                for (int i = 1; i < words.size(); i++) {
                    if (!words.get(i).equals(words.get(i - 1))) {
                        result.add(words.get(i));
                    }
                }
            }
            return result;
        }
    error message: the method get(int) is undefined for the type set(string)



    third part

    since hashset doesn't have index, I not sure what to do here
    I know I suppose to use iterator<String>

    The last time I took cs class was a year ago. so I'm really trying to catch up here.

    Java Code:
     public static ArrayList<String> getOverlap(ArrayList<String> list1,
                                                   ArrayList<String> list2) {
            ArrayList<String> result = new ArrayList<String>();
            int index1 = 0;
            int index2 = 0;
            while (index1 < list1.size() && index2 < list2.size()) {
                int comparison = list1.get(index1).compareTo(list2.get(index2));
                if (comparison == 0) {
                    result.add(list1.get(index1));
                    index1++;
                    index2++;
                } else if (comparison < 0) {
                    index1++;
                } else {  // comparison > 0
                    index2++;
                }
            }
            return result;
        }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: change arraylist to hashSet or TreeSet

    So is the code simply returning the elements that are in both collections?

    If so then look at the Set API.
    It might save you a lot of work...
    Please do not ask for code as refusal often offends.

Similar Threads

  1. question about HashSet and TreeSet
    By j2me64 in forum Advanced Java
    Replies: 4
    Last Post: 03-03-2012, 08:58 PM
  2. HashSet/arraylist
    By jjitss in forum New To Java
    Replies: 1
    Last Post: 11-14-2011, 12:43 AM
  3. change value of arraylist
    By felito in forum New To Java
    Replies: 0
    Last Post: 12-12-2010, 07:16 PM
  4. Replies: 1
    Last Post: 08-25-2010, 07:57 PM
  5. Working with HashSet and TreeSet
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07: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
  •