0% found this document useful (0 votes)
10 views30 pages

Collection Framework in Java - 20240423 - 110131

java collection
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views30 pages

Collection Framework in Java - 20240423 - 110131

java collection
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 30
e Call Us: 011-65164822 & COLLECTION FRAMEWORK CPD TECHNOLOGIES Add:- Block C 9/8, Sector -7, Rohini , Delhi-110085, India www.cpd-india.com = www.blog.cpd-india.com @ COLLECTION FRAMEWORK The collections framework is a unified architecture for representing and manipulating collections, enabling them to be manipulated independently of the details of their representation. It reduces programming effort while increasing performance. It enables interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse. The framework is based on more than a dozen collection interfaces. It includes implementations of these interfaces and algorithms to manipulate them. CONTENTS ° What is Collection? © Collections Framework © Collections Hierarchy © Collections Implementations ° Set © List ° Map OBJECTIVES © Define a collection © Describe the collections framework ° Describe the collections hierarchy ° Demonstrate each collection implementation WHAT IS A COLLECTION? ° A Collection (also known as container) is an object that contains a group of objects treated as a single unit. © Any type of objects can be stored, retrieved and manipulated as elements of collections. COLLECTIONS FRAMEWORK Collections Framework is a unified architecture for managing collections Main Parts of Collections Framework \. Interfaces Core interfaces defining common functionality exhibited by collections 1. Implementations Conerete classes of the core interfaces providing data structures |. Operations Methods that perform various operations on collections COLLECTIONS FRAMEWORK INTERFACES Core Interface Description Collection specifies contract that all collections should implement Set defines functionality for a set of unique elements SortedSet defines functionality for a set where elements are sorted List defines functionality for an ordered list of non- unique elements Map defines functionality for mapping of unique keys to values SortedMap defines functionality for a map where its keys are sorted COLLECTIONS FRAMEWORK IMPLEMENTATIONS HashSet ArrayList HashMap LinkedHashSet lLinkedList LinkedHashMap TreeSet Vector Hashtable ree Map Note: Hashtable uses a lower-case “ @ OPERATIONS Basic collection operations: © Cheek if collection is empty © Check if an object exists in collection. © Retrieve an object from collection © Add object to collection © Remove object from collection © Iterate collection and inspect each object © Each operation has a corresponding method implementation for each collection type COLLECTIONS CHARACTERISTICS ° Ordered © Elements are stored and accessed in a specific order © Sorted © Elements are stored and accessed in a sorted order ° Indexed © Elements can be accessed using an index © Unique © Collection does not allow duplicates a ITERATOR © An iterator is an object used to mark a position in a collection of data and to move from item to item within the collection Syntax: Iterator = .iterator(); COLLECTIONS HIERARCHY SET AND LIST TEIGUG 4 kop loprente: __ Set L List | 4 impiements extends implements es SortedSet | seams implements LinkedHashSet TreeSet Linkosibist Vector COLLECTIONS HIERARCHY MAP COLLECTION IMPLEMENTATIONS List : Lists of things (classes that implement List) Set : Unique things (classes that implement Set) Map : Things with a unique ID (classes that implement Map) | LIST |= = =) A List cares about the index. ArrayList Vector LinkedList LIST IMPLEMENTATIONS ARRAY LIST import java.util ArrayList; public class MyArrayList ( public static void main(String args{ 1) { ArrayList alist = new ArrayList ( ); alist.add(new String("0ne")) ; alist.add(new String(™wo")) ; alist add(new String("Three")}; System.out.printin(alist.get (0): System.out .printin(alist.get (1)); System.out.printin(alist.get(2)): LIST IMPLEMENTATIO VECTOR import java.util. Vector: public class MyVector { public static void main(String argal ]) { Vector vecky = new Vecter( }; vecky.add(new Integer (1)); vecky-add(new Integer (2)) ; vacky.add(new Integer (3)) ; for(int x=0; x<3; xtt) ( System. out-printIn(vecky.get (x)); ) LIST IMPLEMENTATIONS LINKED LIST import java.util LinkedList; public class MyLinkedList ( public static void main(String args[ 1) { Linkedbist link = new Linkedbist( ); Link. add (new Double (2.0); Link-addLast (new Double (3.0)) 7 Link.addPirst (new Double(1.0)) Object array[ ] = link. toArray( ); for (int x80; x<3; xt) ( System. out .printin (array (x) j , SET A Set cares about uniqueness, it doesn’t allow duplicates. HashSet LinkedHashSet TreeSet SET IMPLEMENTATIO. HASH SET import java.uedl.*; public class MyHashset ( public static void main(String args 1) { HashSet hash = new HashSet( ); hash.add("a") hash.add ("b") ; hash. hash. Iterator iterator = hash.iterator( ); while (iterator.hasNext( )) { System.out.printin(iterator.next( )); SET IMPLEMENTATIONS LINKED HASH SET import java.util.LinkedRashset: public class MyLinkedHashset ( public static void main(String axgsf 1) { LinkedHashSet lhs = new LinkedHashSet(); lbs add (new String("One")) ; ths. add (now String("Two")) : Lbs add (new String("Thres")) ; object array[] = 1hs.toarray( } for (int x80; <3; xt) ( System.out .printin (array (x)) ; , T IMPLEMENTATIONS TREE SET public class MyTreeset { public static void main(String args[ 1) { TreeSet tree = new Treeset(); tree.add (“Jody”) ; tree.add("Remiel") ; tree.add ("Reggie") tree.add ("Philippe") ; Iterator iterator = tree.iterator( ); while (iterator.hasNext( )) { System.out.printin(iterator.next( ).toString( }); , key “Pr value “Mark” “John” “Luke” A Map cares about unique identifiers. MAP IMPLEMENTATIONS HASH MAP import java.utdl.HasnMap; public class MyHashMap { public static void main(String argsl 1) { HashMap map = new HashMap( ) map. put ("name", "Jody") ; map.put("id", new Integer (446)): map.put("address", "Manila"); System.out.printin(*Name: " + map.get("name")) ; System.out.printin("ID: " + map.get("id")); System.out.printin("Address: “ + map.get("address")): AP IMPLEMENTATIONS HASH TABLE import java.utilHashtable: public class MyHashtable { public static void main(String args{ 1) { Hashtable table = new Hashtable( ); table.put("name", "Jody") ; table.put("id", new Integer (1001)); table.put("address", new String("Manila")); System.out.printin(*Table of Contents:” + table) ; MAP IMPLEMENTATIONS LINKED HASH MAP import java.util.*; public class MyLinkedHashMap ( public static void main(String arga[ }) { int imum = 0: Linked#ashMap myMap = new LinkedHashMap( ); myMap.put ("name", "Jody") ; myMap . put | , new Integer (446) ); myMap.put "address", "Manila") ; myMap.put("type", "Savings"); Collection values Iterator iterator = myMap.values( ); = values.dterator( ); while(iterator.hasNext()) { System.out.printin(iterator.next( )); , MAP IMPLEMENTATIONS TREE MAP import java.util.*; public class MyTreeMap ( public static void main(String args{]) ( TreeMap treeMap = new TreeMap( ); treeMap.put ("name", “Jody") : treeMap.put("id", new Integer (446)) ; treeMap.put "address", "Manila"); Collection values = treeMap.values() Iterator iterator = values.iterator( ); System, out printin("Printing the VALUES...."); while (iterator.hasNext()) { System.out.println(iterater.next( )); b COLLECTION CLASSES SUMMARY Ordered cary ET HashMap Hashtable TreeMap Sorted By natural order or custom comparison rules LinkedHashMap x By insertion order or No last access order HashSet x No No TreeSet x Sorted By natural order or ‘custom comparison rules LinkedHashSet x By insertion order or No last access order ArrayList X By index Vector X — By index LinkedList X__ By index No KEY POINTS Collections Framework contains: Interfaces » Implementations » Operations A list cares about the index. A set cares about uniqueness, it does not allow duplicates. A map cares about unique identifiers. THANK YOU CPD TECHNOLOGIES Block C 9/8, Sector -7, Rohini, Delhi-110085, India Landmark: Near Rohini East Metro Station, Opposite Metro Pillar No-397 Telephone: 011-65164822 Mobile: +91- 8860352748 Email: [email protected]

You might also like