csf213-l21,22

29
BITS Pilani Hyderabad Campus BITS Pilani Dr.Aruna Malapati Asst Professor Department of CSIS

Upload: nilayan-ahmed

Post on 06-Aug-2015

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSF213-L21,22

BITS PilaniHyderabad Campus

BITS Pilani

Dr.Aruna MalapatiAsst Professor

Department of CSIS

Page 2: CSF213-L21,22

BITS PilaniHyderabad Campus

Java Collection Framework

Page 3: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Collections

• Collections (also called containers)• Collection (i)• Set (i), • HashSet (c), TreeSet (c)• List (i), • ArrayList (c), LinkedList (c)• Map (i), • HashMap (c), TreeMap (c)

Today’s Agenda

Page 4: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• A collection is a general term that means something like "a bunch of objects stored in a structured manner".

• A collection is a group of data manipulate as a single object. Corresponds to a bag.

• Objects are a group of data items that are related to one another.• Ex Email address, Phone numbers

• Java has a collections framework which is a Unified structure for maintaining list.

• Linked list, stacks, Queues

Collection

Page 5: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

The Java Collections Framework consists of:

• Interfaces – You will implement in your data items.

• Classes – these are the concrete implementations of the interfaces. They are reusable data structures.

• Algorithms – these are able to perform useful computations.

What is a collection framework?

Page 6: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Reduce programming effort

• Increase program speed and quality

• Allow interoperability among unrelated APIs

• Reduce effort to learn and use new APIs

• Reduce effort to design new APIs

• Help to reuse the code

Benefits of Collections

Page 7: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Collections are primarily defined through a set of interfaces.

• Supported by a set of classes that implement the interfaces

Interfaces

Collection

Set List Queue

Sorted Set ArrayListLinkedLIst

Map

SotedMap

An unordered collection with no duplicates

An ordered collection with no duplicatesAn ordered collection, duplicates are allowed

An ordered collection, duplicates are allowed

A collection that maps keys to values

A collection ordered by the keys

Page 8: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• import java.util.*or import java.util.Collection;

• There is a sister class, java.util.Collections; that provides a number of algorithms for use with collections: sort, binarySearch, copy, shuffle, reverse, max, min, etc.

Using Collections

Page 9: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Collections Example

import java.util.*; // importing Arrays, List, and Collectionspublic class TestCollections { public static void main(String args[]) { String[] array = {"Phil", "Mary", "Betty", "bob"}; List<String> myList = Arrays.asList(array); Collections.sort(myList); System.out.println("Sorted: " + myList); int where = Collections.binarySearch(myList, "bob"); System.out.println("bob is at " + where); Collections.shuffle(myList); System.out.println("Shuffled: " + myList); }}Sorted: [Betty, Mary, Phil, bob]bob is at 3Shuffled: [Betty, bob, Phil, Mary]

Page 10: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Collection is actually an interface

• Each kind of Collection has one or more implementations

• You can create new kinds of Collections

• When you implement an interface, you promise to supply the required methods

• Some Collection methods are optional– How can an interface declare an optional method?

Collections are interfaces

Page 11: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Collection Interfaces and Classes

Page 12: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Methods in collection interface

Page 13: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

The Collection Interfaces

• The collections framework defines several interfaces

Page 14: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Java provides a set of standard collection classes that implement Collection interfaces.

The Collection Classes

Page 15: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• The collections framework defines several algorithms that can be applied to collections and maps.

• These algorithms are defined as static methods within the Collections class.

The Collection Algorithms

Page 16: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

AlgorithmsDemo.java

Page 17: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.

• Elements can be inserted or accessed by their position in the list, using a zero-based index.

• A list may contain duplicate elements.

• Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.

List Interface

Page 18: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Methods in List interface

Page 19: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

• The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

• Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

The Set interface

Page 20: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Methods in Set interface

SetDemo.java

Page 21: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• The SortedSet interface extends Set and declares the behavior of a set sorted in ascending order.

SortedSetTest.java

The SortedSet interface

Page 22: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.

• Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.

The Map interface

Page 23: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Methods in Map interface

MapInterfaceDemo.java

Page 24: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• The Map.Entry interface enables you to work with a map entry.

• The entrySet( ) method declared by the Map interface returns a Set containing the map entries. Each of these set elements is a Map.Entry object.

The Map.Entry Interface

HashMapDemo.java

Page 25: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Java ArrayList is a resizable array which implements List interface.

• ArrayList provides all operation defined by List interface. 

• ArrayList provides additional methods to manipulate the array that actually stores the elements.

Example:

AddElementToSpecifiedIndexArrayListExample.java

IterateThroughArrayListUsingIteratorExample.java

BinarySearchCharArrayExample.java

ArrayList Class

Page 26: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.

• The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.

• Iterator enables you to cycle through a collection, obtaining or removing elements.

• ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.

How to use an Iterator ?

Page 27: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

The Methods Declared by Iterator

The Methods Declared by ListIterator

IteratorDemo.java

Page 28: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• Both TreeSet and TreeMap store elements in sorted order. However, it is the comparator that defines precisely what sorted order means.

• The Comparator interface defines two methods: compare( ) and equals( ).

• The compare Method• int compare(Object obj1, Object obj2)

• The equals Method• boolean equals(Object obj)

How to use an Comparator ?

ComparableDemo.java

Page 29: CSF213-L21,22

CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

• The Java collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them.

• A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.

• The classes and interfaces of the collections framework are in package java.util.

Summary