Session 5 : Java Collection Framework


Java Collection Framework

Q? what consist of Java Collection Framework
/> Iterable<E> Collection <E> → (Queue<E> , List<E>)
/> Map<K,V> → HashMap, TreeMap
/> the most used collection classes are : ArrayList and LinkedList which implements interface List
/> the most used map collection classes are : HashMap and TreeMap which implements interface Map
/> also we use class : HashSet and TreeSet which implements interface Set
/> Collection implements the iterable interface while helps us in iteration

Q? what is this Iterable interface
/> this interface is mostly used to iterate the container of elements
/> this interface consist of single method: Iterator<E> iterator() : return the iterator on the elements

Q? what is the use of iterator interface
/> this provides a advanced mechanism to loop through all the elements of a Collection bucket
/> auto increment of the element occurs whereas it also has the mechanism to keep track of last visited elements

Q? what is iterator class
/> this class allow the iteration on the elements of a collection:
/> it consist of two main methods:
boolean hasNext() : checks if there is a next element to iterate on
E next() : return the next element and advances by one position
ex:
User u = new User(nick,first,last,email);

for(Iterator<User> i=users.iterator();i.hasNext();){
User u1 = i.next();// the new next object is assigned here
System.out.print("Printed: "+u1);// to check
}

Q? what does Collection interface contains
/> this consists of the group of elements which consists of the refrences to the objects
/> all this refrences to objects may be ordered/disordered , duplicated/ not duplicated
/> some of the most used methods of this collection interface are:
int size() // gives the size of the collection
boolean contains(E element ) // check out if the collection contain following object element
boolean add(E element); // we add new collection element here
ex:
Collection<Product> products = new ArrayList<Product>();
products.add(new Product("abc"));
if(product.contains(abc)){
System.out.println("product: abc"+ products.size());
}

Q? what is List interface
/> list interface extends collection interface and can contain duplicate elements
/> order of insertion is perserved
/> elements are accessed by position
/> it consist of two most used classes : ArrayList and LinkedList

Q? what is the difference between the ArrayList implementation and the LinkedList implementation
/> In ArrayList to use get(n) method we take Constant time
and to use add() method we also take Constant time ;
where as In LinkedList to use get(n) method we take Linear time
and to use add() method we take Constant time;
So the idea is that ArrayList class are better when we use the get(n) method to get the specific object
while the LinkedList class are better when we use the add() method to add the elements to the collections.

Note : Queue & Set interface is not discussed : check out the Java Documentation
Note : Do not modify the collection (add/del) while you are iterating over a colleciton : we have to use the iterator methods to do such activities : like Iterator.add() ; Iterator.remove();


Q? what is this Map Interface
/> we associate Key to Values in Map object
/> both the Key and Values must be objects
/> Key must be unique i.e. not repeated and only one value is associated with the key
/> some of the most used methods of the Map Interface are:
V put(K key, V value) ; //K and V are object types
V get(K key)
boolean containsKey(K key)
public Set<K> keySet()
public Collection<V> values()

Ex :
Map<String,Cliente> clienti = new TreeMap<String,Cliente>();
String codice = “abc”;
clienti.put(codice, new Cliente(codice));
if (clienti.containsKey(codice)) throw new Exception("codice esistente");

/> Two most used class in Map are :
1> HashMap : no order of key (elements are traversed according to keys )
get/put takes constant times
2> TreeMap : ascending key order (elements are traversed according to keys natural ordering )


Q? how objects are sorted
/> To sort the objects we have to compare between two objects so for this case we use Comparable interface.
This interface is in packages java.lang and java.util

public interface Comparable<T>{
public int compareTo(T obj);
}








public int compareTo(Object o) {
Train t = (Train) o;
// this is for descendant order
int diff = t.year - this.year;
if (diff!=0) return diff; // return the int difference value
diff = t.month-this.month;
if (diff!=0) return diff;
diff = t.day-this.day;
if (diff!=0) return diff;
return 0;
}


/> this interface has just one method i.e. compareTo();
/> this method compares object1 with object2;
object1.compareTo(object2);
If the return value of this method is:
<0 this(object1) precedes (object2)
==0 this(object1) has the same order as (object2)
>0 this(object1) follows (object2)
Here the Idea is :
/> String object are lexicographically ordered (like in dictionary i.e. alphabetically)
/> number and sub-classes are ordered numerically


Note:* some of the satic methods to do Sort in java.util.Collections class that works on lists are:
void sort(List<T> list);
void sort(List<T> list, Comparator<? Super T>) // using the comparator object

List balls = new LinkedList();
balls.add(new Ball("red",1234));
balls.add(new Ball("orange",4231));
balls.add(new Ball("blue",3210));
Collections.sort(balls); // sort by name of ball
Collections.sort(balls, new BallIDComparator()); // sort by id of ball

Comments

Popular Posts