Application & Script

Home Field Realtors 
KJB Realty

Flow of Control
Java Classes
User-Defined Classe
Java Applet
Java Arrays
Java Exceptions
Java.io
Recursion
Java performance Tips

Java Frameworks
Java Collections

Hosting Customers
Chicago limo Services
Website Design
Naperville Map

Ubuntu vurtual Host Setup

Java Collection interface



Represent different types of collections, such as sets, lists , link lists, arrays (not the primitive arrays), maps, etc. A collection is an object that contains another object. All elements of a collections are objects where by objects can be sotred in a collection (Array for example), retreived and aggrate operations can be performed on it.

Elements of the collection

 

Collections are classified based on their structures and capabilities: Bags, Sets, Lists, Maps. The core interfaces that are the basis for all collections are Collection, Set, List, Iterator, SotedSet, Map and SortedMap.

Interfaces

Abstract Classes

Bags

Un-ordered un-sorted and allow duplicates. Least restrictive, General form, Collection interface is used in Java to implement bags.

Maps

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
They don’t extend Collection interface or the Set interface

Map map = new HashMap();
map.put (“a”, “X”); // map = {a->X}
map.put (“b”, “Y”); // map = {a->X, b->Y}
map.put (“c”, “X”); // map = {a->X,b->Y,c->X}
map.put (“a”, “Z”); // map = {a->Z,b->Y,c->X}
map.remove (“a”); // map = {b->Y,c->X}

public interface Map<K,V> {

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();
void putAll(Map<? extends K, ? extends V> m);
void clear();
public Set<K> keySet();
public Collection<V> values();
public Set<Map.Entry<K,V>> entrySet();
public interface Entry { K getKey();
V getValue();
V setValue(V value);
}

}

Implementation (The Collection Interface):

public interface Collection<E> extends Iterable<E> {

int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional
boolean remove(Object element); //optional
Iterator<E> iterator(); // Bulk operations
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); //optional
boolean removeAll(Collection<?> c); //optional
boolean retainAll(Collection<?> c); //optional
void clear(); //optional
// Array operations
Object[] toArray();
<T> T[] toArray(T[] a);

}


The Set Intercae

public interface Set<E> extends Collection<E> {

int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional boolean
remove(Object element); //optional
Iterator<E> iterator(); // Bulk operations
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); //optional
boolean removeAll(Collection<?> c); //optional
boolean retainAll(Collection<?> c); //optional
void clear(); //optional //
Array Operations Object[] toArray();
<T> T[] toArray(T[] a);

}

Implementations For Sets: HahSet, LinkedHashSet, TreeSet
Implementations For Lists: ArrasyList, LinkedList
Implementations For Maps: HashMap, LinkedHashMap, IdentityHashMap and TreeMap

Classes that implement the Collection interface are called CONCRETE Collection


Iterators for Collections


The Iterator Pattern is used to provide a uniformed way to iterate through different concrete collections. 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 Iterator for a collection by calling its iterator method. Each concrete collection provides a concrete iterator that implements the Iterator interface or/and the ListIterator interface. The following is the Iterator interface.

public interface Iterator<E> {

boolean hasNext(); //true if there are more elements
E next(); // returns the next element in the iteration
void remove(); // remove last element returned

}

Iterator iter = set.iterator();
while (iter.hasNext())
String s = (String) iter.next();

}

Set set = new HashSet();
set.add(“foo”); // set = {“foo”}
set.add (“bar”); // set = {“foo”, “bar”}
int n = set.size(); //n=2
if (set.contains(“foo”)
System.out.println (“foo is in”);

public interface ListIterator<E> extends Iterator<E>

Adds: boolean hasNext(), boolean hasPrevious() , E previous() , int nextIndex() , int previousIndex() , … (Adds the ability to go backwards)