P. 1
Collection API

Collection API

|Views: 34|Likes:
Published by api-3749401

More info:

Published by: api-3749401 on Oct 16, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

For inserting, make a call to the setSize ().

class CloneExample1 implements Cloneable { static CloneExample1 b; static int z = 10; public static void main(String[] args) { CloneExample1 a = new CloneExample1(); try { Object o = a.clone(); b = (CloneExample1)o; } catch(Exception e) {} 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 a) the storage mechanism used b) in the way they can access data and c) in the rules about what the date might be stored.

-

The java.util.Collections Class is the main class, which provides static methods, which operate on Collections or return a Collection. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection. 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 simple collection places no constraints on the type of elements, order of elements or repetition of elements within 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.

-

-

-

The 4 widely used techniques / storage mechanism used are: Array Storage: a) b) c) d) Fast to access relatively inefficient if elements need to be inserted or deleted in the middle of the list. no special search mechanism . 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 a) b) c) allows elements to be added to or removed from the collection at any location in the container. allows for the size of the collection to grow arbitrarily. This is because each element in an individual object that refers to the next (and sometimes to the previous) elements in the List.

d) Tree a) b) Hashing a) b)

significantly slower to access by index than an array and no special mechanism to search.

This is like a linked list but insists on a means of ordering In a Tree, Indexing is slow, but searching is fast.

This requires that some unique identifying key to be associated with each data item, which provides efficient searching. Fastest in retrieving items

Concrete Implementation in Collection API 1) Hashtable / HashMap : Both use Hashing mechanism of storage. Hashtable was present right from the beginning in the JDK, but HashMap was introduced in JDK 1.2. 2) HashSet: This is a Set, so it does not permit duplicates and uses hashing for storage. 3) LinkedList: This is an implementation of a List based on linked list storage mechanism. 4) TreeMap: This class provides an ordered Map. The elements must be orderable, either by implementing the Comparable interface or by providing a Comparator class to perform comparisons. 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 This provides a compare method, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap). 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 This interface takes place of Enumeration in Java Collection framework. The ways in which Iterators differ from Enumeration is that Iterators allow caller to remove elements by using the method remove(). The methods in this interface are: a) b) c) boolean hasNext(); Object next() 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: a) b) c) Object previous () – returns the previous object in the list. void remove () – void add ()

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->