CORNELL CS 211 - Java Collections Framework

Unformatted text preview:

1Java Collections FrameworkCS211Fall 20002Java Collections Framework■ Collections: holders that let you store and organize objects in useful ways for efficient access■ Since Java 1.2, the package java.util includes interfaces and classes for a general collection framework■ Goal: conciseness● A few concepts that are broadly useful● Not an exhaustive set of useful concepts■ Two types of concepts are provided● Interfaces (i.e., ADTs)● Implementations3JCF Interfaces and Classes■ Interfaces● Collection● Set (no duplicates)● SortedSet● List (duplicates OK)● Map (i.e., Dictionary)● SortedMap● Iterator● ListIterator■ Classes● HashSet● TreeSet● ArrayList● LinkedList● HashMap● TreeMap4java.util.Collection (an interface)public int size( );Return number of elements in collectionpublic boolean isEmpty( );Return true iff collection holds no elementspublic boolean add (Object x);Make sure the collection includes x; returns true if collection has changed (some collections allow duplicates, some don’t)public boolean contains (Object x);Returns true iff collection contains x (uses equals( ) method)public boolean remove (Object x);Removes a single instance of x from the collection; returns true if collection has changedpublic Iterator iterator ( );Returns an Iterator that steps through elements of collection5java.util.Iterator (an interface)public boolean hasNext ( );Returns true if the iteration has more elementspublic Object next ( );Returns the next element in the iteration; throws NoSuchElementException if no next elementpublic void remove ( );The element most-recently returned by next( ) is removed from the collection; can throw IllegalStateException if next( ) not yet used or if remove( ) already called6Additional Methods of Collectionpublic Object [ ] toArray ( );Returns a new array containing all the elements of this collectionpublic Object [ ] toArray (Object [ ] dest);Returns an array containing all the elements of this collection; uses dest as that array if it canBulk Operations:public boolean containsAll (Collection c);public boolean addAll (Collection c);public boolean removeAll (Collection c);public boolean retainAll (Collection c);public void clear ( );27java.util.Set (an interface)■ Set extends Collection● Set has no methods of its own, but it inherits the methods from Collection■ A Set contains no duplicates● If you attempt to add( ) an element twice then the second add( ) will return false (i.e., the Set has not changed)■ Write a method that checks if a given word is within a Set of words■ Write a method that removes all words longer than 5 letters from a Set■ Write methods for the union and intersection of two Sets8Set Implementations■ java.util.HashSet (a hashtable)● Constructorspublic HashSet ( );public HashSet (Collection c);public HashSet (int initialCapacity);public HashSet (int initialCapacity, float loadFactor);■java.util.TreeSet (a balanced BST [red-black tree])● Constructorspublic TreeSet ( );public TreeSet (Collection c);…■Both implement


View Full Document

CORNELL CS 211 - Java Collections Framework

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Java Collections Framework
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Collections Framework and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Collections Framework 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?