Download presentation
Presentation is loading. Please wait.
Published byLilian Reed Modified over 9 years ago
1
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit
2
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
3 Hierarchy of Collection Framework
4
4 Benefits of the Java Collections Framework Reduces programming effort. Increases program speed and quality. Fosters software reuse.
5
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
6 Iterator Interface To retrieve the elements one by one from a collection object. Three methods: -boolean hasNext () -element next () -void· remove ()
7
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
8 Difference between Iterator and ListIterator IteratorListIterator Only forward directionForward and Backward direction Less preferredMore preferred
9
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
10 List Interface Ordering Duplicate Operation Manages its own Size
11
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
12 ArrayList LinkedList Array
13
13 Set Interface Set is an group element arranged like an Array. Grow dynamically. Not allow duplicate elements.
14
14 Difference Bitween Set and Maps SetList Doesn’t allow duplicatesAllow duplicates Unordered collectionOrdered collection
15
15 Set Methods add() clear() contains() size() isEmpty() iterator() Remove()
16
16 Various Implementaion of Set Interface HashSet TreeSet LinkedHashSet
17
17 HashSet Set of elements(objects). Uses HashTable to store the elements. Extends abstract class and implements Set interface. Contains unique elements only.
18
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
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
20 TreeSet Contains unique elements. Implements NavigableSet interface. Extends SortedSet interface. Maintains ascending order.
21
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
22 LinkedHashSet Subclass of HashSet. Generic class. Declaration: -class LinkedHashSet Internally uses a linked list to store the element.
23
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
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
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
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
27 How to use Map in Java
28
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
29 Comparable Interface To order the objects. java.lang package. compareTo(Object). Single sorting sequence.
30
30 Comparable Interface public int compareTo(Object obj) public void sort(List list)
31
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
32 Comparator Interface To order the objects. compare(Object obj1,Object obj2). equals(Object element). Multiple sorting sequence.
33
33 Comparator Interface public int compare(Object obj1,Object obj2) public void sort(List list,Comparator c)
34
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
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
36 Introduced in J2SE-5 Deals with type-safe objects Syntax : ClassOrInterface Eg. ArrayList JAVA GENERICS
37
37 ADVATAGES OF JAVA GENERICS 1. Type -safety 2. Type -casting Not Required 3. Compile time checking
38
Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.