collection notes

Upload: sasirekha-perumalvijayan

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Collection Notes

    1/2

    consider you have a collection of country names.1.India2.Australia3.England4.NewZealand5.South Africa

    Problem1:You need to insert the above list of countries in thesame order given above.

    What type of collection can be used?

    solution:Any ordered collection can be used:Example List

    Problem 2:Suppose you want to automatically sort thecountry names and store it ina collection ?What type of collection can be used?

    Solution :A sorted collection like SortedSet can be used whichautomatically sorts and rearranges the value each time

    a new country is added to the list.

    Note:A sortedMap is a Map that maintains its entries inascending order,sorted based on the key.

    List interfaceThis is an interfaceAn ordered collection which contains duplicate elementsIt permits multiple null values.

    set interfaceA collection that cannot contain duplicate elements.Only one null value is allowed.

    HashsetAllows null.Does not allow duplicate values.

    TreesetAllows entries to be sorted in specific order

    ArrayListimplement List interface

    ArrayList can grow and shrink in size dynamically based on the elementstored hence can be considered as a variable array.

    values are stored internally as a array hence random insert and retrievalof elements are allowed.

    can be traversed using a foreach loop , iterator or indexes.

    initially gets created with an initial capacity, when this gets filled the listautomatically grows.

    can hold only object type data-cannot hold primitive type values

  • 7/29/2019 Collection Notes

    2/2

    supports duplicate entries.

    HashMap

    can contain null valuesno thread safe

    Hashtablecannot contain nul valuesThis is a thread safe

    TreeMapstores in sorted order according to the keyNot thread safe.

    VectorClassThe underlying Data structure for the vector is resizable array or growable array.Insertion order is preserved.Duplicate objects are allowed.null insertion is possible.Heterogeneous objects are allowed.

    Best choice if the frequent operation is retrieval.Worst choice if the frequent operation is insertion or deletion in the middle.Vector class implemented serializable, cloneable and RandomAccess Interfaces.

    Difference between vector and ArrayList?We can get synchronized version of ArrayList of by using the following method ofcollection class.Ex:Vector ArrayList1) All methods of vector are synchronized 1) no method is synchronized2) vector object is thread safe 2) vector object is not thread safe by default

    3) performance is low 3) performance is High4) 1.0 version(Legacy) 4) 1.2 version(non Legacy)