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

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Stacks, Queues, and Linked Lists
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Interfaces in Java’s Collection Framework Rick Mercer.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
List Interface and Linked List Mrs. Furman March 25, 2010.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
University of Limerick1 Collections The Collection Framework.
Collections Dwight Deugo Nesa Matic
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Java Collections CHAPTER 3-February-2003
Slides by Donald W. Smith
Chapter 16: Linked Lists.
Using the Java Collection Libraries COMP 103 # T2
STACKS & QUEUES for CLASS XII ( C++).
C++ Programming:. Program Design Including
Sixth Lecture ArrayList Abstract Class and Interface
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Stacks Chapter 5.
Design & Analysis of Algorithm Map
Chapter 19 Java Data Structures
Chapter 20 Lists, Stacks, Queues, and Priority Queues
JAVA COLLECTIONS LIBRARY
Data Structures Interview / VIVA Questions and Answers
Introduction to Linked Lists
structures and their relationships." - Linus Torvalds
Java Collections Overview
Arrays and Linked Lists
List Data Structure.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Java Collections Framework
14.1 The java.util Package.
CSE 1030: The Collection Frameworks
CSE 1020: The Collection Framework
Data Structures & Algorithms
structures and their relationships." - Linus Torvalds
Chapter 20 Lists, Stacks, Queues, and Priority Queues
LINEAR DATA STRUCTURES
Introduction to Java Collection
Presentation transcript:

Collections

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

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

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

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.

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.

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…

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.

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.

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).

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.

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.

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

Simple Tree Example Simple Hashing Example

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){ }

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.

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.

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.

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

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

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