Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collections. 012345 ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.

Similar presentations


Presentation on theme: "Collections. 012345 ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional."— Presentation transcript:

1 Collections

2 012345 ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional arrays and linked lists. circular linked list

3 Diff. between an array and linked list Advantages/Disadvantages of array: Accessing an element is fast Adding or deleting an element is very tedious and slow Fixed size Array elements are stored sequentially in memory Advantages/Disadvantages of linked list: Adding or deleting an element is fast But Accessing an element is slower. Size can grow or shrink linked list elements are scattered across memory

4 Data structures in Java are named as Collections. Collections are the data structures used to store, retrieve, search, update, delete elements. Java.util.Collection interface 1.List interface extended from Collection 2.Set interface extended from Collection 3.Map is an interface List has two implementations ArrayList and LinkedList. ArrayList and LinkedList are classes. Set has two implementations HashSet and TreeSet Map has two different implementations, HashMap and TreeMap

5 Characteristics of List 1.Can store duplicate objects 2.Store elements in the same order as added i..e maintains insertion order 3.Supports ListIterator and Iterator to traverse thru the elements What is an Iterator? An Iterator is used to traverse thru the individual elements of any Collection, such as List, Set or Map. Using Iterator we can traverse only in forward direction. Using ListIterator, we can traverse in forward and backward direction.

6 Difference between ArrayList and LinkedList An ArrayList is internally implemented based on traditional arrays. Hence accessing an element is fast, but deletion/growing/shrinking is slower. A LinkedList is internally implemented based on traditional linked list. Hence deletion/addition/growing/shrinking is faster, but accessing an element is slower.

7 What is purpose of Collections class? Collections class in java.util provides static utility methods to perform manipulations(like reversing, shuffling, searching, etc…) on any Collection, such as ArrayList, LinkedList, HastSet, etc…

8 Set characteristics A Set does not store duplicate objects Does not maintain insertion order Can be traversed only in one direction, using Iterator Difference between HashSet and TreeSet HashSet stores objects by following Hashing techniques, internally. Hence objects are stored in random order based on corresponding Hash value. TreeSet stores objects in sorted order, by using Tree based algorithms. TreeSet, by default uses natural order for sorting the elements. It is possible to customize sorted order, by using Comparator interface.

9 Difference between Enumeration and Iterator Using Iterator it is possible to traverse thru any Collection, and also allows to set() or remove() an element. Enumeration is Legacy one, used to traverse elements, but cannot be used to update an element.

10 HashSet and TreeSet Difference 1.Ordering : HashSet stores the object in random order. There is no guarantee that the element we inserted first in the HashSet will be printed first in the output. Elements are sorted according to the natural ordering of its elements in TreeSet 2.Null value : HashSet can store null object while TreeSet does not allow null object. If one try to store null object in TreeSet object, it will throw Null Pointer Exception. 3.Performance : HashSet take constant time performance for the basic operations like add, remove contains and size.While TreeSet guarantees log(n) time cost for the basic operations (add,remove,contains).

11 HashSet and TreeSet Difference 4. Functionality : TreeSet is rich in functionality as compare to HashSet. Functions like pollFirst(), pollLast(),first(),last(),ceiling(),lower() etc. makes TreeSet easier to use than HashSet. 5. Comparison : HashSet uses equals() method for comparison in java while TreeSet uses compareTo() method for maintaining ordering.

12 HashMap & TreeMap: Differences 1.HashMap returns unordered values. TreeMap returns the elements in ascending order (known as natural order) of keys by default. TreeMap order can be customized using Comparator.Comparator 2.The performance of HashMap is higher than TreeMap because TreeMap requires minimum execution of two method calls to create a tree structure and to print the elements in natural order.

13 A ServerSocket is for accepting incoming network connections on some stream protocol; e.g. TCP/IP. A DatagramSocket is for sending and receiving datagrams on some connectionless datagram / message protocol; e.g. UDP/IP

14 23 12 615 42 35 41 12 23 Simple Tree Example Simple Hashing Example

15 Generics A Generic class is a class, whose code is not written for a specific data type. Rather code is written for a generic type, and the actual required data type is being specified, when the object creation is done. Note that compiler generates the code required for specific type. Advantage of generics is 1.It avoids code duplication 2.Improves code reusability Collection Framework has both generic and non generic implementation. How to use wildcard in Generics Unknown Wildcard: List means a list typed to an unknown type. This could be a List, a List, a List etc. public void processElements(List elements){ } extends Wildcard Boundary:List means a List of objects that are instances of the class A, or subclasses of A (e.g. B and C). public void processElements(List elements){ } super Wildcard Boundary List means that the list is typed to either the A class, or a superclass of A. public static void insertElements(List list){ }

16 Difference between Generic and Non Generic Collection 1.Non Generic Collection are based on Object class, and casting is done during run time, which may affect performance. Where as generic Collection are for the specified class type only. 2.Using Non Generic Collection we can store Heterogenous objects(i..e objects of different class type), where as Generic Collection allow Homogenous objects only.

17 How to customize sorted order of TreeSet or TreeMap This can be achieved with Comparator or Comparable. Using Comparator the original class need not be changed. Hence it is possible to have multiple sorting logics, each one can be used for different purpose or scenarios. For eg. Student objects can be sorted as per marks when ranked, but for attendance purpose Student objects can be sorted in alphabetical order of Names. But this is not directly possible with Comparable.

18 java.util.Map A Map is used to store Key, Value pairs, unlike List or Set. Here Both Kay and Value elements can be objects. There are two implementations of Map, they are HashMap and TreeMap. In HashMap elements are stored based on hash value of Key object. In TreeMap elements are stored in sorted order of Key object.

19 Legacy classes Vector Vector is synchornized, and ArrayList is not synchronized Vector exists, since first release of Java Now Vector also implements from Collection interface Vector provides methods of Collection as well as legacy methods of Vector Stack The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty. Queue Queue is an interface, and represents FIFO(First In First Out) operations Properties

20 A ServerSocket is for accepting incoming network connections on some stream protocol; e.g. TCP/IP. A DatagramSocket is for sending and receiving datagrams on some connectionless datagram / message protocol; e.g. UDP/IP

21 Arithmetic Operators +, -, *,/,%, ++,-- Logical &&,||,! Assignment =, +=, -=,*=,/=,%= Eg. Comparison or Relation, =,==,!= Bitwise&,|,^, >


Download ppt "Collections. 012345 ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional."

Similar presentations


Ads by Google