0% found this document useful (0 votes)
32 views2 pages

Java Collections Framework Java Collections Framework: CS211 Fall 2000

The Java Collections Framework provides interfaces and classes for organizing objects into collections. The main interfaces are Collection, Set, SortedSet, List, and Map. Collection is the root interface extending it are Set which prohibits duplicates, and List which allows duplicates. Common implementations are HashSet, TreeSet, ArrayList and LinkedList. The Iterator interface is used to traverse the elements of a collection.

Uploaded by

Abdul Gafur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Java Collections Framework Java Collections Framework: CS211 Fall 2000

The Java Collections Framework provides interfaces and classes for organizing objects into collections. The main interfaces are Collection, Set, SortedSet, List, and Map. Collection is the root interface extending it are Set which prohibits duplicates, and List which allows duplicates. Common implementations are HashSet, TreeSet, ArrayList and LinkedList. The Iterator interface is used to traverse the elements of a collection.

Uploaded by

Abdul Gafur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Collections Framework Java 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 q A few concepts that are broadly useful q Not an exhaustive set of useful concepts Two types of concepts are provided q Interfaces (i.e., ADTs) q Implementations

CS211 Fall 2000

JCF Interfaces and Classes


s

java.util.Collection (an interface)


public int size( ); Return number of elements in collection public boolean isEmpty( ); Return true iff collection holds no elements public boolean add (Object x); Make sure the collection includes x; returns true if collection has changed (some collections allow duplicates, some dont) 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 changed public Iterator iterator ( ); Returns an Iterator that steps through elements of collection
4

Interfaces q Collection q Set (no duplicates) q SortedSet q List (duplicates OK)


q q

Classes q HashSet q TreeSet q ArrayList q LinkedList


q q

Map (i.e., Dictionary) SortedMap Iterator ListIterator

HashMap TreeMap

q q

java.util.Iterator (an interface)


public boolean hasNext ( ); Returns true if the iteration has more elements public Object next ( ); Returns the next element in the iteration; throws NoSuchElementException if no next element public 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 called

Additional Methods of Collection


public Object [ ] toArray ( ); Returns a new array containing all the elements of this collection public Object [ ] toArray (Object [ ] dest); Returns an array containing all the elements of this collection; uses dest as that array if it can Bulk Operations: public boolean containsAll (Collection c); public boolean addAll (Collection c); public boolean removeAll (Collection c); public boolean retainAll (Collection c); public void clear ( );
6

java.util.Set (an interface)


s

Set Implementations
s

Set extends Collection q Set has no methods of its own, but it inherits the methods from Collection A Set contains no duplicates q 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

java.util.HashSet (a hashtable) q Constructors


public HashSet ( ); public HashSet (Collection c); public HashSet (int initialCapacity); public HashSet (int initialCapacity, float loadFactor);

s
s

Write methods for the union and intersection of two Sets

java.util.TreeSet (a balanced BST [red-black tree]) q Constructors


public TreeSet ( ); public TreeSet (Collection c);

s
8

Both implement Cloneable

You might also like