Unit -5
There are two ways
to traverse collections:
(1) with the
(2) by using
The following code uses the
An Iterator is an
object that enables you to traverse through a collection and to remove elements
from the collection selectively, if desired. You get an
The Collection Interface
A collection
represents a group of objects known as its elements.
The
Collection interface is used to pass around collections of
objects where maximum generality is desired.
For example, by
convention all general-purpose collection implementations have a constructor
that takes a
Collection argument.
This constructor,
known as a conversion constructor, initializes the new collection to
contain all of the elements in the specified collection, whatever the given
collection's sub interface or implementation type.
In other words, it allows you to convert the
collection's type.
List<String> list = new
ArrayList<String>(c);
The following shows
the Collection interface.public interface Collection<E> extends Iterable<E> {
// Basic operations
int size();
boolean isEmpty();
boolean contains(Object element);
// optional
boolean add(E element);
// optional
boolean remove(Object element);
Iterator<E> iterator();
// Bulk operations
boolean containsAll(Collection<?> c);
// optional
boolean addAll(Collection<? extends E> c);
// optional
boolean removeAll(Collection<?> c);
// optional
boolean retainAll(Collection<?> c);
// optional
void clear();
// Array operations
Object[] toArray();
<T> T[] toArray(T[] a);
}
Traversing Collections
(1) with the
for-each construct and(2) by using
Iterators. * for-each Construct
Thefor-each construct allows you to
concisely traverse a collection or array using a for loop
The for Statement. The following code uses the
for-each construct to print out each
element of a collection on a separate line.for (Object o : collection)
System.out.println(o);
Iterators
Iterator for a collection by calling
its iterator method. The following is
the Iterator interface.public interface Iterator<E> {
boolean hasNext();
E next();
void remove(); //optional
}
The hasNext method returns true if the iteration has more elements, and the next method returns the next element in the
iteration. The remove method removes the last
element that was returned by next from the underlying Collection. The remove method may be called only
once per call to next and throws an exception if
this rule is violated.Collection Interface Bulk Operations
Bulk operations perform an operation on an entireCollection. You could implement these
shorthand operations using the basic operations, though in most cases such
implementations would be less efficient. The following are the bulk operations:containsAll— returnstrueif the targetCollectioncontains all of the elements in the specifiedCollection.addAll— adds all of the elements in the specifiedCollectionto the targetCollection.removeAll— removes from the targetCollectionall of its elements that are also contained in the specifiedCollection.retainAll— removes from the targetCollectionall its elements that are not also contained in the specifiedCollection. That is, it retains only those elements in the targetCollectionthat are also contained in the specifiedCollection.clear— removes all elements from theCollection.
--------------------------------------------------------------------------------
The Set Interface
A set is a collection that cannot contain duplicate elements.
It models the mathematical set abstraction. The
The following is
the Set interface contains only
methods inherited from Collection and adds
the restriction that duplicate elements are prohibited. Set also adds a stronger
contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared
meaningfully even if their implementation types differ. Two Set instances are equal if they
contain the same elementsSet interface.public interface Set<E> extends Collection<E> {
// Basic operations
int size();
boolean isEmpty();
boolean contains(Object element);
// optional
boolean add(E element);
// optional
boolean remove(Object element);
Iterator<E> iterator();
// Bulk operations
boolean containsAll(Collection<?> c);
// optional
boolean addAll(Collection<? extends E> c);
// optional
boolean removeAll(Collection<?> c);
// optional
boolean retainAll(Collection<?> c);
// optional
void clear();
// Array Operations
Object[] toArray();
<T> T[] toArray(T[] a);
}
The Java platform contains three general-purpose
Set
implementations: HashSet, TreeSet, and LinkedHashSet
HashSet, which stores its elements in a hash table, is the
best-performing implementation; however it makes no guarantees concerning the
order of iteration.
Treeset, which stores
its elements in a red-black tree, orders its elements based on their values; it
is substantially slower than
HashSet.
Linked Hashedset which is implemented as a hash table with a
linked list running through it, orders its elements based on the order in which
they were inserted into the set (insertion-order).
-----------------------------------------------------------------------------------------------
The List Interface
A
list is an ordered collection (sometimes called a sequence). Lists may
contain duplicate elements. In addition to the operations inherited from Collection, the List
interface includes operations for the following:
- Positional access — manipulates elements based on their numerical position in the list
- Search — searches for a specified object in the list and returns its numerical position
- Iteration — extends Iterator semantics to take advantage of the list's sequential nature
- Range-view — performs arbitrary range operations on the list.
The
List interface follows.
public interface List<E>
extends Collection<E> {
// Positional access
E get(int index);
// optional
E set(int index, E element);
// optional
boolean add(E element);
// optional
void add(int index, E element);
// optional
E remove(int index);
// optional
boolean addAll(int index, Collection<? extends E> c);
// Search
int indexOf(Object o);
int lastIndexOf(Object o);
// Iteration
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
// Range-view
List<E> subList(int from, int to);
}
The
Java platform contains two general-purpose List
implementations. ArrayList, which is usually the better-performing
implementation, and Linked List which offers better performance under certain
circumstances. Also, Vector has been retrofitted to implement List.
---------------------------------------------------------------------------------------------------------------
The Map Interface
A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction. TheMap interface follows.public interface Map<K,V> {
// Basic operations
V put(K key, V value);
V get(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
// Bulk operations
void putAll(Map<? extends K, ? extends V> m);
void clear();
// Collection Views
public Set<K> keySet();
public Collection<V> values();
public Set<Map.Entry<K,V>> entrySet();
// Interface for entrySet elements
public interface Entry {
K getKey();
V getValue();
V setValue(V value);
}
}
---------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment