1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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.
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.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
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.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
05 - Containers DRAFT COPY © S. Uchitel, 2004 Container OrderedDuplicates BagsNOYES SetsNONO ListsYESYES MapsNO.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Chapter 19 Java Data Structures
Java's Collection Framework
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
(c) University of Washington14-1 CSC 143 Java Collections.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Data structures and algorithms in the collection framework 1.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Java 2 Collections Bartosz Walter Software Engineering II.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
16-August-2002cse Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
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.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Java Collections and Generics 1. Collections Collections in java is a framework that provides an architecture to store and manipulate the group of objects.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Java Collections Framework The client view. The Big Picture.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Java Collections CHAPTER 3-February-2003
Chapter 19 Java Data Structures
Java Collections Overview
Introduction to Collections
Introduction to Collections
14.1 The java.util Package.
Introduction to Collections
Collections Framework
JCF Collection classes and interfaces
Introduction to Java Collection
Presentation transcript:

1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit

2 Introduction to Collections  Collections in java is a framework that provides an architecture to store and manipulate the group of objects.  Data Operations like:  Searching  Sorting  Insertion  Manipulation  Deletion

3 Hierarchy of Collection Framework

4 Benefits of the Java Collections Framework  Reduces programming effort.  Increases program speed and quality.  Fosters software reuse.

5 Some Functions Related To Map CLEAR- Removes all elements from map GET- Retrieve values of Requested key KEYSET- Returns set that contains all keys REMOVE- Removes requested Key and it’ values from Map SIZE- Returns number of key value pairs in the map EQUALS- Compares an object with Map for Equality

6 Iterator Interface  To retrieve the elements one by one from a collection object.  Three methods: -boolean hasNext () -element next () -void· remove ()

7 ListIteartor Interface  To retrieve the elements from a collection object, both in forward and reverse directions.  Important methods: -boolean hasNext () -boolean has Previous () -element next () -element previous () -void remove ()

8 Difference between Iterator and ListIterator IteratorListIterator Only forward directionForward and Backward direction Less preferredMore preferred

9 Example ArrayList al = new ArrayList(); // use iterator to display contents of al system.out.print("original contents of al: "); Iterator itr = al.Iterator(); while(itr.hasnext()) { object element = itr.next(); system.out.print(element + " "); } // Modify objects being iterated ListIterator litr = al.listIterator(); while(litr.hasNext()) { Object element = litr.next(); litr.set(element + "+"); } System.out.print("Modified contents of al: "); itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } System.out.println();

10 List Interface  Ordering  Duplicate  Operation  Manages its own Size

11 Java's List interface Java also has an interface java.util.List to represent a list of objects. It adds the following methods to those in Collection : (a partial list) Some are: public void add(int index, Object o) public Object get(int index) public Object remove(int index) public Object set(int index, Object o)

12  ArrayList  LinkedList Array

13 Set Interface  Set is an group element arranged like an Array.  Grow dynamically.  Not allow duplicate elements.

14 Difference Bitween Set and Maps SetList Doesn’t allow duplicatesAllow duplicates Unordered collectionOrdered collection

15 Set Methods  add()  clear()  contains()  size()  isEmpty()  iterator()  Remove()

16 Various Implementaion of Set Interface  HashSet  TreeSet  LinkedHashSet

17 HashSet  Set of elements(objects).  Uses HashTable to store the elements.  Extends abstract class and implements Set interface.  Contains unique elements only.

18 HashSet Example HashSet al=new HashSet (); al.add("Ravi"); al.add("Vijay"); Iterator itr=al.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); }

19 HashSet  Syntax: - HashSet al=new HashSet ();  Create hashset object:- -HashSet assetSet=new HashSet(); // HashSet insance without any element -HashSet from ArrayList=new HashSet(Arrayas.asList("Java","c++); //copying content -HashSet properSet=new HashSet(50); //HashSet with initial capacity

20 TreeSet  Contains unique elements.  Implements NavigableSet interface.  Extends SortedSet interface.  Maintains ascending order.

21 TreeSet  Syntax: -TreeSet al=new TreeSet ();  Example: TreeSet al=new TreeSet (); al.add("Ravi"); al.add("Vijay"); Iterator itr=al.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); }

22 LinkedHashSet  Subclass of HashSet.  Generic class.  Declaration: -class LinkedHashSet  Internally uses a linked list to store the element.

23 Map Defination and Types  MAP is an object that maps keys to values.  Restrictions: -No duplicate keys are allowed in map. -Each key can map to at most one value.  TYPES- 1.HashMap 2.TreeMap 3.Linked HashMap

24 TreeSet  Syntax: -TreeSet al=new TreeSet ();  Example: TreeSet al=new TreeSet (); al.add("Ravi"); al.add("Vijay"); Iterator itr=al.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); }

25 Diferences Bitween Different Maps  HashMap has no guarantee about it’s Iteration Order.  TreeMap Iterates according to their natural ordering of the keys.  Linked HashMap Iterates according to the order in which the entries were put into the map.

26 TreeSet  Syntax: - TreeSet al=new TreeSet ();  Example: TreeSet al=new TreeSet (); al.add("Ravi"); al.add("Vijay"); Iterator itr=al.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); }

27 How to use Map in Java

28 Some Functions Related To Map CLEAR- Removes all elements from map GET- Retrieve values of Requested key KEYSET- Returns set that contains all keys REMOVE- Removes requested Key and it’ values from Map SIZE- Returns number of key value pairs in the map EQUALS- Compares an object with Map for Equality

29 Comparable Interface  To order the objects.  java.lang package.  compareTo(Object).  Single sorting sequence.

30 Comparable Interface  public int compareTo(Object obj)  public void sort(List list)

31 Comparable Interface class Student implements Comparable{ int rollno; String name; int age; Student(int rollno,String name,int age) { this.rollno=rollno; this.name=name; this.age=age; } public int compareTo(Object obj){ Student st=(Student)obj; if(age==st.age) return 0; else if(age>st.age) return 1; else return -1; } S Student.java AgeComparator.java Collections.sort(al);

32 Comparator Interface  To order the objects.  compare(Object obj1,Object obj2).  equals(Object element).  Multiple sorting sequence.

33 Comparator Interface  public int compare(Object obj1,Object obj2)  public void sort(List list,Comparator c)

34 Comparator Interface SStudent.javaAgeComparator.jav a class AgeComparator implements Comparator{ public int Compare(Object o1,Object o2){ Student s1=(Student)o1; Student s2=(Student)o2; if(s1.age==s2.age) return 0; else if(s1.age>s2.age) return 1; else return -1; a } Collections.sort(al,new AgeComparator()); Collections.sort(al,new NameComparator()); class Student{ int rollno; String name; int age; Student(int rollno,String name,int age) { this.rollno=rollno; this.name=name; this.age=age; }

35 Difference between Comparable and Comparator ComparableComparator Single sorting sequenceMultiple sorting sequence Affects the original classDoesn't affect the original class compareTo()compare() java.langjava.util Collections.sort(List)Collections.sort(List,Comparator)

36 Introduced in J2SE-5 Deals with type-safe objects Syntax : ClassOrInterface Eg. ArrayList JAVA GENERICS

37 ADVATAGES OF JAVA GENERICS 1. Type -safety 2. Type -casting Not Required 3. Compile time checking

Thank You