<< Chapter < Page Chapter >> Page >

Different classes, different implementations

You can safely call the contains method on any object instantiated from a class that properly implements the Collection interface, even if you don't know the actual type of the collection object.

The manner in which the search will be performed will probably differ from one concrete implementation of the interface to the next. For example, a TreeSet object will perform the search very rapidly with a time cost of only log(n) where n is the number of elements. On the other hand, for the same numberof elements, because of a different underlying data structure, a search on an ArrayList object will probably require more time than a search on a TreeSet object. As the number of elements increases, the difference in time cost between the two will also increase.

A sample program

Consider the sample program shown in Listing 1 . This program compares the search speed of the ArrayList class and the TreeSet class. A detailed discussion of the program follows Listing 1 .

Listing 1 . SpeedTest01.
/*File SpeedTest01 Copyright 2001 R.G.Baldwin**************************************/ import java.util.*;public class SpeedTest01{ public static void main(String args[]){ new Worker().doIt();}//end main() }//end class SpeedTest01class Worker{ public void doIt(){int size = 2000000; //Create a TreeSet objectCollection aTree = new TreeSet(); //Populate the TreeSet object with// random values. The add() method // for a set rejects duplicates.Random rnGen = new Random(); for(int ct = 0; ct<size; ct++){ aTree.add(new Double(rnGen.nextDouble()));}//end for loop //Create and populate an ArrayList// object with the same random // valuesCollection aList = new ArrayList(aTree); //Extract a value near the center// of the ArrayList object to use // as a test case.Object testVal = ((List)aList).get(size/2); //Search for the test value in each// of the collection objects. // Measure and display the time// required to perform the search // in each case.long start = new Date().getTime(); boolean found = aList.contains(testVal);long stop = new Date().getTime(); System.out.println(found + " " + (stop - start));start = new Date().getTime(); for(int x = 0; x<100000; x++){ found = aTree.contains(testVal);}//end for loop stop = new Date().getTime();System.out.println(found + " " + (stop - start)/100000.0); }//end doIt()}// end class Worker

Instantiate and populate a TreeSet object

The program begins by instantiating a TreeSet object and populating it with approximately 2,000,000 elements as shown in Listing 2 . The values encapsulated in the objects referred to by the elements in the collection areproduced by a random number generator.

Recall that the add method of a Set object rejects duplicate elements, so there may be fewer than 2,000,000 elements in the object after it ispopulated, depending on how many of the random values are duplicates.

Listing 2 . Beginning of the doIt method.
public void doIt(){ int size = 2000000;Collection aTree = new TreeSet(); Random rnGen = new Random();for(int ct = 0; ct<size; ct++){ aTree.add(new Double(rnGen.nextDouble()));}//end for loop

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask