Java 5
Java 5
3
Collection categories
• Ordered Collection
– The objects within the collection are maintained in a particular
order or not
– When an object is added or removed from an ordered
collection , the order is automatically maintained
• Duplicate Elements
– Whether or not the collection allows duplicate objects
– Collection will automatically reject an attempt to add an object
5
Methods in List Interface
• void add(object o)
• void addAll(Collection c)
• void clear()
• boolean contains(object)
• Object get(int index)
• int indexOf(object o)
• ListIterator listIterator()
• Object set (int index, Object element)
List Implementations
• Array List
– a resizable-array implementation
• unsynchronized
• Linked List
– a doubly-linked list implementation
– if elements frequently inserted/deleted within the List
• May provide better performance than ArrayList
– For queues and double-ended queues (deques)
• Vector
– a synchronized resizable-array implementation of a List with additional
"legacy" methods.
ArrayList
• Permits all elements, including null.
• ArrayList()
– Constructs an empty list with an initial capacity of ten.
• ArrayList(int initialCapacity)
– Constructs an empty list with the specified initial capacity.
Generics
• Introduced in Java 5.0
• Can put any Object in to Collections, because they hold Just Objects
– Type-safe Collections are created ,ensuring type-safety at compile time
rather than run-time
• Type erasure Applied For :
– Stronger type checks at compile time.
– Elimination of casts.
ArrayList<Employee> alist=new
ArrayList<Employee>();
Employee ramesh = new Employee(101,”Ramesh”);
alist.add(ramesh);
10
Two Schemes of Traversing Collections
• for-each
– System.out.println(o);
• Iterator
Method Usage
}
}
Array List
• When to use ArrayList
15
The ListIterator Interface
• Extends the Iterator interface to provide for the two-way traversal of a List
collection (forward and backward)
• The most frequently used are:
Method Usage
haxNext() Returns true if this list iterator has more
elements when traversing the list in the
forward direction
hasPrevious() Returns true if this list iterator has more
elements when traversing the list in the
backward direction
next() Returns the next element in the list (as a
generic Object)
1. equals,
2. compare
3. compareTo,
4. hashCode
• Collection that depends on hashing requires both equality testing and hash
codes
• If the List consists of String elements, it will be sorted into alphabetical order
• Imposes a total ordering on the objects of each class that implements it.
– Collections.sort(arrList);
Using a separate Comparator
• Can put the comparison method in a separate class that implements
Comparator instead of Comparable
if(obj1.getBookno()<obj2.getBookno())return -1;
if(obj1.getBookno()>obj2.getBookno())return 1;
return 0;
}
}
Implementing Multiple Comparators
if(obj1.getBookno()<obj2.getBookno())return -1;
if(obj1.getBookno()>obj2.getBookno())return 1;
return 0;
}
}
bkList.add(bk1);
bkList.add(bk2);
bkList.add(bk3);
Collections.sort(bkList, bc);
for(Book bk:bkList)
{
System.out.println(bk.getBookno());
}
}
When to use each
• The Comparable interface is simpler and less work
– The Object of the class implements Comparable
– A public int compareTo(Object o) method is overriden
– When no argument constructor is used in TreeSet or TreeMap
– easy to add/delete
• Un-Ordered
28
equals” operation of “Set” Interface
• Set also adds a stronger contract on the behavior of the equals and
hashCode operations,
• Two Set instances are equal if they contain the same elements
hs.add(b1);
hs.add(b2);
for(Book bk:hs)
{
System.out.println(bk.getBookno());
System.out.println(bk.getBookname());
}
}
Tree Set With Comparators
• Tree Set Class overloaded constructors one without argument and another
one which takes comparator as arguments.
tscomp.add(bk1);
tscomp.add(bk2);
tscomp.add(bk3);
}
Collection Hierarchy- Map
32
Why Map is not Extending Collection
• Maps work with key/value pairs
– As a set of keys,
– A collection of values
• Tree Map
– A balanced binary tree implementation
– Imposes an ordering on its elements based on comparable/compartor
• Hash table
– Synchronized hash table implementation of Map interface.
– Doesn't support null keys and values
Hash Map
• Works on the principal of hashing.
36
Working of Hash Map
• put()
– Invoked by passing key-value pair,
– It uses the hashCode() to find the index in the array and then use
equals() method to find the correct Entry and return its value.
Deciding the Collection Interface
38