Collections Framework

Slides:



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

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
15-Jun-15 Lists in Java Part of the Collections Framework.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
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.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
List Interface and Linked List Mrs. Furman March 25, 2010.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Fundamental of Java Programming
OOP Tirgul 5.
Marcus Biel, Software Craftsman
Chapter 19 Java Data Structures
Software Development Java Collections
Chapter 20 Lists, Stacks, Queues, and Priority Queues
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Chapter 17 Object-Oriented Data Structures
Introduction to Collections
Programming in Java Lecture 11: ArrayList
Introduction to Collections
Collections in Java The objectives of this lecture are:
14.1 The java.util Package.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
Introduction to Collections
ArrayLists 22-Feb-19.
CSE 1020: The Collection Framework
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists 27-Apr-19.
Hashing in java.util
Part of the Collections Framework
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Presentation transcript:

Collections Framework Unit III-Chapter No. : 7-Collections Framework Collections Framework

Topic Learning Outcomes List the various interfaces provided by collection framework Explain methods defined by list Interface. Explain methods defined by sorted list Illustrate the methods defined by Set and Map Interface School of Computer Science & Engineering

School of Computer Science & Engineering Content Introduction to Collection Framework, Interfaces and classes Exploring List Interface Exploring List and set interface implementation classes Exploring Map Interface School of Computer Science & Engineering

School of Computer Science & Engineering Collections: Basics Define Goals Algorithms Interfaces Collection List Set Map Classes Examples School of Computer Science & Engineering

School of Computer Science & Engineering Collections: Define The Collections Framework is a hierarchy of interfaces and classes to manage group of objects. manage Create group Delete group Update group Add Delete Insert Sort Search Algorithms: are another important part of the collection framework, they operate on collections and are defined as static methods within the collection class (sort, search, reverse etc.,). They are available to all collections. School of Computer Science & Engineering

School of Computer Science & Engineering Collections: Goals High Performance We seldom need to code Collections manually, the implementations for the fundamental Collections are highly efficient (we use most of the predefined collection classes to manage group of objects). The framework had to allow different types of collections to work in a similar manner. Extending and/or adapting a collection had to be easy. The collection Framework is built upon a set of standard interfaces: Set, List, Map And also we can implement our own Collection. School of Computer Science & Engineering

Collections-Interfaces: Collection It is at the top of all the collections. It defines common methods. Method Name Description Boolean add(E obj) Adds objects to invoking collection, returns true if added, false if it fails. Boolean addAll(Collection c) Adds all the elements of c to the invoking collection Void clear() Removes all the elements from the invoking collection Boolean contains(Object obj) Returns true if the invoking collection contains object, returns false if it does not contain Int size() Returns the number of elements held in the invoking collection Boolean remove(Object obj) Removes specified object from collection and returns true if it is success else false Boolean removeAll(Collection c) Removes all elements of c from the invoking collection School of Computer Science & Engineering

Collections-Interfaces: List The List interface extends Collection interface It stores sequence of elements. Elements can be inserted/accessed by their position- zero based index A List may contain duplicate elements Syntax: interface List <E> // E is the type of objects the list holds. Method Description void add(int index, E obj) Inserts ‘obj’ into the invoking list at the specified index. boolean addAll(int index, Collection c) Inserts ‘c’ into the invoking collection starting from ‘index’ E get(int index) Returns ‘object (of type E)’ from the invoking collection at the specified index. Int indexOf(Object obj) Returns the object from invoking collection at the specified index E remove(int index) Removes and returns object on the invoking collection at the specified index. School of Computer Science & Engineering

Collections-Interface: Set The Set interface defines a set Extends Collection. It does not allow duplicates The add method returns ‘false’ if an attempt is made to add duplicate elements to set. It does not define any additional methods of its own (it inherits from collection) Syntax: interface Set <E> School of Computer Science & Engineering

Collections-Interface: Map A ‘map’ is an object that stores associations between ‘key’ and ‘value’. Both ‘key’ and ‘value’ are objects. The ‘key’ must be unique but the values may be duplicate Rollno unique and name can be duplicate Some maps can accept null key and null values. The Map interface maps unique keys to values. The ‘key’ is an object that you use to retrieve a value at a later time. Given ‘key’ and ‘value’ we can store the data in ‘map’ School of Computer Science & Engineering

School of Computer Science & Engineering Collections- Classes AbstractCollection: Implements most of the Collection interface AbstractList: Extends AbstractCollection and implements List interface ArrayList: Extends AbstractList and implements dynamic array. AbstractSet: Extends AbstractCollection and implements most of the Set interface. HashSet: Extends AbstractSet LinkedHashSet: Extends HashSet to allow inter-order iterations TreeSet: Extends AbstractSet. Implements a set sorted in a tree. School of Computer Science & Engineering

Collections- Classes: ArrayList Syntax : class ArrayList <E> ArrayList supports dynamic array, that can grow as needed. Standard arrays are of fixed length. ArrayList is variable length array of object references. Constructors ArrayList() ArrayList(int capacity) ArrayList (Collection <? Extends E> c) School of Computer Science & Engineering

ArrayList Collection: Example ArrayList to store integer type School of Computer Science & Engineering

School of Computer Science & Engineering ArrayList: Example School of Computer Science & Engineering

ArrayList: user defined type School of Computer Science & Engineering

HashSet (Set interface): Example HashSet extends AbstractSet and implements set interface HashSet does not define any additional methods beyond those provided by its superclass and interface. Declaration: class HashSet<E> Constructors HashSet() HashSet(Collection <? Extends E> c) HashSet(int capacity) School of Computer Science & Engineering

School of Computer Science & Engineering HashSet: Example School of Computer Science & Engineering

HashSet: Example (User Defined type) School of Computer Science & Engineering

School of Computer Science & Engineering Map interface A ‘map’ is an object that stores associations between ‘key’ and ‘value’. Both ‘key’ and ‘value’ are objects. The ‘key’ must be unique but the values may be duplicate Rollno unique and name can be duplicate Some maps can accept null key and null values. The Map interface maps unique keys to values. The ‘key’ is an object that you use to retrieve a value at a later time. Given ‘key’ and ‘value’ we can store the data in ‘map’ School of Computer Science & Engineering

HasMap (Map implementation): Example School of Computer Science & Engineering

School of Computer Science & Engineering LinkedList The LinkedList class extends AbstractSequentialList and implements the List, Deque, Queue interfaces. It provides a linked-list data structure. Declaration of LinkedList class Linkedlist <E> // Here ‘E’ is the type of object the collection holds. Constructors LinkedList() LinkedList(Collection<? Extends E> c) Methods addFirst() addLast() getFirst() getLast() , removeFirst() , removeLast() School of Computer Science & Engineering

School of Computer Science & Engineering LinkedList: Example School of Computer Science & Engineering

School of Computer Science & Engineering LinkedList: Example School of Computer Science & Engineering