Professional Documents
Culture Documents
Collection
Collections
A collection sometimes called a container is
simply an object that groups multiple elements into a
single unit(called container). Collections are used to
store, retrieve, manipulate, and communicate aggregate
(group of data)data.
Collection's works a bit like arrays, except their size can
change dynamically, and they have more advanced
behaviour than arrays.
Collections Framework
Java.util.Collection interface
A Java collection is any class that holds objects and implements the Collection
interface
For example, the ArrayList<T> class is a Java collection class, and implements all the methods
in the Collection interface
This idiom creates a new ArrayList (an implementation of the List interface), initially containing
all the elements in c.
List<String> list = new ArrayList<String>(c);
List is the interface
ArralyList is the class that can implement of method of List interface.
The Collection interface is the highest level of Java's framework for collection
classes
All of the collection classes discussed here can be found in package java.util
Dynamic Binding
(late binding, virtual binding)
Introduction to Collections
Collections
Types of Collection
While you can get all the details from the Java API, you are
expected to learn (i.e. memorize):
13
14
Generalisation
Interfaces
Aspecial
Collectionthat
cannotcontain
duplicates.
Set
SortedSet
Rootinterfaceforoperations
commontoalltypesofcollections
Storesmappingsfromkeysto
values
Collection
List
Map
SortedMap
Queue
Specialisescollectionwith
operationsforFIFOandpriority
queues.
Specialmapin
whichkeysare
ordered
Storesasequenceofelements,
allowingindexingmethods
SpecialSetthatretains
orderingofelements.
Specialisation
15 251
SOFTENG
Object Oriented
16
boolean add(E o)
boolean contains(Object o)
boolean remove(Object o)
boolean isEmpty()
int size()
Object[] toArray()
Iterator<E> iterator()
boolean hasNext()
E next()
void remove()
18
boolean contains(Object o)
boolean containsAll(Collection<?> c)
boolean addAll(Collection<? extends E> c)
boolean retainAll(Collection<?> c)
boolean removeAll(Collection<?> c)
// membership test
//subset test
// union
// intersection
// difference
20
21
In the program code given below, we have taken a string of elements. We have
converted this string of elements into a list of array and then we have applied the
hasNext() method which returns true because there are more elements in the list.
Hence we get the following output.
import java.util.*;
public class hasNext{
public static void main (String args[]) {
boolean b;
String elements[] = {"Blue", "Grey", "Teal"};
Set s = new HashSet(Arrays.asList(elements));
Iterator i = s.iterator();
if (b = i.hasNext()){
System.out.println(b);
}
}
}
C:\unique>javac hasNext.java
C:\unique>java hasNext
true
C:\unique>
22
23
import java.util.*;
public class next{
public static void main (String args[]) {
String elements[] = {"Blue", "Grey", "Teal"};
Set s = new HashSet(Arrays.asList(elements));
Iterator i = s.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}
24
C:\unique>javac next.java
C:\unique>java next
Blue
Grey
Teal
C:\unique>
25
E first()
E last()
26
Set<K> keySet()
Collection<V> values()
27
K firstKey()
K lastKey()
28
Generics
Example 1 Defining Generic Types:
public interface List<E> {
void add(E x);
Iterator<E> iterator();
}
public interface Iterator<E> {
E next();
boolean hasNext();
}
public interface Map<K,V> {
V put(K key, V value);
}
29
Java generics
lets you write code that is safer and easier to read
is especially useful for general data structures, such as
ArrayList
generic types are a powerful tool to write reusable objectoriented components and libraries
however, the generic language features are not easy to
master and can be misused
34
35
Collection Utils
Handful Collection utils appears as static methods
of the class Collections:
http://java.sun.com/javase/6/docs/api/java/util/Collections.html
36
Some implementations
38
The End
39