Professional Documents
Culture Documents
b.met1(z);
a.met1(z);
System.out.println(z);
System.out.println(a.hashCode());
System.out.println(b.hashCode());
}
int met1(int q)
{
++q;
z = q;
return z;
}
Collection API
- The Collection API is nothing but a set of data structures in Java. There are
standard interfaces and implementation class and all the collections vary
according to
- The general interface java.util.Collection defines the basic framework for all
collections. This interface has methods that add item into the collection,
remove items from the collection, determine whether the items are in the
collection and count the number of items in the collection.
- A collection in which items are ordered (e.g, in the order in which they are
added) is known as a List. A List is ordered and does not reject duplicates.
- A collection in which the condition is that it cannot contain the same value
more than once, it is known as a Set. A Set has no special order but rejects
duplicates.
- The final type of specialized behavior directly supported by the Collection API
is known as a Map. In a Map, it uses the set of key values to look up or index
the stored date.
- IMP: List, Set and Map Interface extend the Collection Interface.
Array Storage:
a) Fast to access
b) relatively inefficient if elements need to be inserted or deleted in the
middle of the list.
c) no special search mechanism .
d) Array will be a appropriate choice for date that are ordered, do not
change often and do not need to be searched much.
Linked List
Tree
Hashing
2) HashSet: This is a Set, so it does not permit duplicates and uses hashing
for storage.
5) TreeSet: This class provides an ordered set, using a tree for storage. As
with the TreeMap, the elements must have an order associated with them.
Interface Comparator
Interface Comparable
This interface imposes a total ordering on the objects of each class that
implements it. This ordering is referred to as the class's natural ordering, and the
class's compareTo method is referred to as its natural comparison method.
Lists (and arrays) of objects that implement this interface can be sorted
automatically by Collections.sort (and Arrays.sort). Objects that implement this
interface can be used as keys in a sorted map or elements in a sorted set,
without the need to specify a comparator.
Interface Iterator
a) boolean hasNext();
b) Object next()
c) void remove()
ListIterator
This interface has methods that you can use to traverse a collection of object
backwards or forwards. This enables that the object can be retrieved more than
once. The important methods are: