Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programing: Compare times per single search between HashSet and TreeSet col

ID: 640183 • Letter: J

Question

Java Programing:

Compare times per single search between HashSet and TreeSet collections.

Select and store words from 2 books by the same author in 2 separate sets (use www.gutenberg.org). Use one of these sets to search word-by-word in the other set. Measure time per each search. Show results from this experiment along with your conclusion. Also submit code.

Use these two books by Charles Dickens:

https://www.gutenberg.org/files/24022/24022-0.txt

https://www.gutenberg.org/cache/epub/1400/pg1400.txt

Explanation / Answer

for (String s : words)

{

   Validator.passesDirtyWordCheck(s);

}

public static boolean passesDirtyWordCheck(String word)

{

  final String delimeters = "[\:\_\-\.\/\,\|]";

  String lower = word.toLowerCase();

  if (lower.contains("f***"))

    return false;

  if (dirtyWords_ == null)

    loadValidationFields();

  String [] words = lower.split(delimeters);

  for (String s : words)

  {

    if(Collections.binarySearch(dirtyWords_, s) < 0)

      return true;

    //if(dirtyWords_.contains(s))

    // return false;

  }

  return false;

  //return true;

}

10M elements

============

class java.util.ArrayList: 78

class java.util.LinkedList: 94

class java.util.ArrayList: 53

class java.util.LinkedList: 102

class java.util.ArrayList: 52

class java.util.LinkedList: 101

20M elements

============

class java.util.ArrayList: 249

class java.util.LinkedList: 947

class java.util.ArrayList: 182

class java.util.LinkedList: 945

class java.util.ArrayList: 184

class java.util.LinkedList: 1899

import java.util.*;

final class AListVsLList {

    static final Object obj = new Object();

    static final int count = 40 * 1000 * 1000;

    static void test(List<Object> list) {

        long start = System.currentTimeMillis();

        for (int i = 0 ; i < count ; i++)

            list.add(obj);

        long elapsed = System.currentTimeMillis() - start;

        System.out.println(list.getClass() + ": " + elapsed);

    }

    public static void main(String... args) throws Exception {

        test(new ArrayList<Object>());

        System.gc();

        Thread.sleep(1000);

        test(new LinkedList<Object>());

        System.gc();

        Thread.sleep(1000);

        System.out.println();

        test(new ArrayList<Object>());

        System.gc();

        Thread.sleep(1000);

        test(new LinkedList<Object>());

        System.gc();

        Thread.sleep(1000);

        System.out.println();

        test(new ArrayList<Object>());

        System.gc();

        Thread.sleep(1000);

        test(new LinkedList<Object>());

        System.gc();

        Thread.sleep(1000);

        System.out.println();

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote