Web Design & Development Lecture 6

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

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.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  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.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
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.
The Java Collections Package C. DeJong Fall 2001.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Java's Collection Framework
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Data structures Abstract data types Java classes for Data structures and ADTs.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
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.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Collections Dwight Deugo Nesa Matic
Object Oriented Programming in Java Habib Rostami Lecture 7.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Using the Java Collection Libraries COMP 103 # T2
Efficiency of in Binary Trees
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Java Collections Overview
Introduction to Collections
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
CS2110: Software Development Methods
Introduction to Collections
ArrayLists 22-Feb-19.
Collections Framework
Manu Kumar CS193J: Programming in Java Summer Quarter 2003 Lecture 3 Collections and More OOP Manu Kumar Thursday,
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
slides created by Marty Stepp
Creating and Modifying Text part 3
Hashing in java.util
Part of the Collections Framework
Presentation transcript:

Web Design & Development Lecture 6

Collections

Collections Built-in support for collections Similar to STL in C++ Collection type Sequence/Set Example ArrayList Map type Hashtable/dictionary Example HashMap Collections store references to objects Use inheritance and interfaces Read http://java.sun.com/docs/books/tutorial/collections

Collection Design All classes provides almost same methods get(), size(), isEmpty()… Easy learning curve for using Collections Implemented as reference to Object Similar to using a void * in C Require a cast back (down casting) to the actual type Example String element = (String)arraylist.get(i) Remember, Java checks all casts at run-time

Collection Messages Basic messages constructor() int size() Creates a collection with no elements int size() Number of elements in the collection boolean add() Add a new reference/element at the end of the collection Returns true is the collection is modified. iterator() Returns an Iterator Object

Additional Collection Messages Utilities Additional useful methods boolean isEmpty() boolean contains(Object o) Iterative search, uses equals() boolean remove(Object o) Iterative remove(), uses equals() Boolean addAll(Collection c)

ArrayList

ArrayList Replaces the “Vector” Can grow over time Methods add(Object) Can add all kinds of objects implicit upcasting int size() Object get(int index) Index is from 0 to size() -1 Must cast to appropriate type when used. remove(index) Removes object reference at the specified index iterator() We’ll see an example!

Example TestArrayList. java /* ArrayList in this example, is used to store Student objects. We are using the same Student class which we build in our previous lectures */ import java.util.*; public class TestArrayList { public static void main (String args[ ]){ //Create ArrayList object ArrayList al = new ArrayList(); Student s1 = new Student(“ali”, 1); Student s2 = new Student(“saad”, 2); Student s3 = new Student(“raza”, 3); al.add( s1 ); al.add( s2 ); al.add( s3 ); //continue..

Example TestArrayList. java //checking whether arraylist contains student objects or not boolean b = al.isEmpty(); if (b == true){ System.out.println("arraylist is empty"); } else { int size = al.size(); System.out.println("arraylist size: "+ size); // continue..

Example TestArrayList. java //using loop to iterate for (int i=0; i< al.size(); i++) { Student s = (Student) al.get(i); s.print(); } } //end of main

Compile & Execute

HashMap

HashMap Stores key & value in pair form Cannot contain duplicate keys Implements the Map interface Keys and Values are stored as Objects put(Key, Value) implicit upcasting Object get(Key) Must cast to appropriate type when used. int size()

Example TestHashMap. java /* HashMap is used to store Student objects as value and rollnos as keys. We are using the same Student class which we build in our previous lectures */ import java.util.*; public class TestHashMap { public static void main (String args[]){ HashMap h = new HashMap(); Student s1 = new Student(“ali”, 1); Student s2 = new Student(“saad”, 2); Student s3 = new Student(“raza”, 6); h.put("one", s1 ); h.put("two", s2 ); h.put("six", s6 ); //continue..

Example TestHashMap. java boolean b = h.isEmpty(); if (b == true){ System.out.println("hashmap is empty"); } else { int size = h.size(); System.out.println("hashmap size:"+ size); Student s = (Student) h.get("two"); s.print(); } //end of main

Compile & Execute

Putting All Together

We have learned So far How to Perform IO Selection and Control Structures OOP Collections So, lets do a small problem

Problem Address Book Wants to store name, address, phone no of a Person Features Add Delete Search (on name) Exit (from application) The above listed features must be available to user in the form of JOptionPane based MENU.

Approach for Solving Problem Step 1 Make a Person class with name, address, phone no attributed Step 2 Make a AddressBook class Use ArrayList to store Person’s Obejcts Write methods add, delete and Search Use JOptionPane methods for IO Step 3 Make a Test class (Driver Program) Build Menu using switch selection structure Call appropriate methods of AddressBook

Lets Start Coding!