13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Map Collections and Custom Collection Classes Chapter 14.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
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.
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
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.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Lecture 4 The Java Collections Framework. Java Container Classes.
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.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Chapter 18 Java Collections Framework
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Collections –data structures and Algorithms L. Grewe.
Data structures and algorithms in the collection framework 1.
Collection 105 Yola. To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
1 Interfaces in Java’s Collection Framework Rick Mercer.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
1 Java Programming II Collections. 2 Java Programming II Contents  Collections Framework  Collections of Objects  Iterable and Iterator  Comparable.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Starting Out with Java From Control Structures through Data Structures by Tony Gaddis and Godfrey Muganda Collections in Java.
AD Lecture #3 Java Collection Framework 1.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
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.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
Software Development Java Collections
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
Java Collections Overview
Java语言程序设计 马 皓
14.1 The java.util Package.
Collections Framework
CSE 1020: The Collection Framework
Presentation transcript:

13 Collections Framework

2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map

3 Objectives Define a collection Describe the collections framework Describe the collections hierarchy Demonstrate each collection implementation

4 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.

5 Collections Framework Collections Framework is a unified architecture for managing collections Main Parts of Collections Framework 1.Interfaces Core interfaces defining common functionality exhibited by collections 2.Implementations Concrete classes of the core interfaces providing data structures 3.Operations Methods that perform various operations on collections

6 Collections Framework Interfaces Core InterfaceDescription 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

7 Collections Framework Implementations SetListMap HashSetArrayListHashMap LinkedHashSetLinkedListLinkedHashMap TreeSetVectorHashtable TreeMap Note: Hashtable uses a lower-case “t”

8 Collections Framework Operations Basic collection operations: Check 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

9 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

10 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();

11 Collections Hierarchy Set and List HashSet Collection SortedSet List Set LinkedHashSetTreeSet LinkedList Vector ArrayList implements extends

12 Collections Hierarchy Map Map SortedMap Hashtable LinkedHashMap HashMap TreeMap implements extends

13 Set: Unique things (classes that implement Set) Map: Things with a unique ID (classes that implement Map) List: Lists of things (classes that implement List) Collection Implementations Next!

14 A List cares about the index. List “Paul” “Mark” “John” “Paul” “Luke” value index LinkedListVectorArrayList

15 List Implementations ArrayList import java.util.ArrayList; public class MyArrayList { public static void main(String args[ ]) { ArrayList alist = new ArrayList( ); alist.add(new String("One")); alist.add(new String("Two")); alist.add(new String("Three")); System.out.println(alist.get(0)); System.out.println(alist.get(1)); System.out.println(alist.get(2)); } Back! One Two Three

16 List Implementations Vector import java.util.Vector; public class MyVector { public static void main(String args[ ]) { Vector vecky = new Vector( ); vecky.add(new Integer(1)); vecky.add(new Integer(2)); vecky.add(new Integer(3)); for(int x=0; x<3; x++) { System.out.println(vecky.get(x)); } Back!

17 List Implementations LinkedList import java.util.LinkedList; public class MyLinkedList { public static void main(String args[ ]) { LinkedList link = new LinkedList( ); link.add(new Double(2.0)); link.addLast(new Double(3.0)); link.addFirst(new Double(1.0)); Object array[ ] = link.toArray( ); for(int x=0; x<3; x++) { System.out.println(array[x]); } Back!

18 A Set cares about uniqueness, it doesn’t allow duplicates. Set “Paul” “Mark” “John” “Luke” “Fred” “Peter” TreeSetLinkedHashSetHashSet

19 Set Implementations HashSet import java.util.*; public class MyHashSet { public static void main(String args[ ]) { HashSet hash = new HashSet( ); hash.add("a"); hash.add("b"); hash.add("c"); hash.add("d"); Iterator iterator = hash.iterator( ); while(iterator.hasNext( )) { System.out.println(iterator.next( )); } Back! dacbdacb

20 Set Implementations LinkedHashSet import java.util.LinkedHashSet; public class MyLinkedHashSet { public static void main(String args[ ]) { LinkedHashSet lhs = new LinkedHashSet(); lhs.add(new String("One")); lhs.add(new String("Two")); lhs.add(new String("Three")); Object array[] = lhs.toArray( ); for(int x=0; x<3; x++) { System.out.println(array[x]); } Back! One Two Three

21 Set Implementations TreeSet import java.util.TreeSet; import java.util.Iterator; public class MyTreeSet { public static void main(String args[ ]) { 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.println(iterator.next( ).toString( )); } Back! Jody Philippe Reggie Remiel

22 A Map cares about unique identifiers. Map “Paul” “Mark” “John” “Paul” “Luke” key value “Pl” “Ma” “Jn” “ul” “Le” LinkedHashMapTreeMapHashtable HashMap

23 Map Implementations HashMap import java.util.HashMap; public class MyHashMap { public static void main(String args[ ]) { HashMap map = new HashMap( ); map.put("name", "Jody"); map.put("id", new Integer(446)); map.put("address", "Manila"); System.out.println("Name: " + map.get("name")); System.out.println("ID: " + map.get("id")); System.out.println("Address: " + map.get("address")); } Back! Name: Jody ID: 446 Address: Manila

24 Map Implementations Hashtable import java.util.Hashtable; public class MyHashtable { public static void main(String args[ ]) { Hashtable table = new Hashtable( ); table.put("name", "Jody"); table.put("id", new Integer(1001)); table.put("address", new String("Manila")); System.out.println("Table of Contents:" + table); } Back! Table of Contents: {address=Manila, name=Jody, id=1001}

25 Map Implementations LinkedHashMap import java.util.*; public class MyLinkedHashMap { public static void main(String args[ ]) { int iNum = 0; LinkedHashMap myMap = new LinkedHashMap( ); myMap.put("name", "Jody"); myMap.put("id", new Integer(446)); myMap.put("address", "Manila"); myMap.put("type", "Savings"); Collection values = myMap.values( ); Iterator iterator = values.iterator( ); while(iterator.hasNext()) { System.out.println(iterator.next( )); } Back! Jody 446 Manila Savings

26 Map Implementations TreeMap 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.println("Printing the VALUES...."); while (iterator.hasNext()) { System.out.println(iterator.next( )); } Back! Printing the VALUES.... Manila 446 Jody

27 Collection Classes Summary NoBy indexXLinkedList NoBy indexXVector NoBy indexXArrayList NoBy insertion order or last access order XLinkedHashSet By natural order or custom comparison rules SortedXTreeSet No XHashSet NoBy insertion order or last access order XLinkedHashMap By natural order or custom comparison rules SortedXTreeMap No XHashtable No XHashMap SortedOrderedListSetMapClass

28 Key Points Collections Framework contains: 1.Interfaces 2.Implementations 3.Operations A list cares about the index. A set cares about uniqueness, it does not allow duplicates. A map cares about unique identifiers.