CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]

Slides:



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

June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
Using Maps. A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
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.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
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.
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.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
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.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply.
(c) University of Washington14-1 CSC 143 Java Collections.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
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.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Data structures Abstract data types Java classes for Data structures and ADTs.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
The Java Collections Framework Based on
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:
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Maps Nick Mouriski.
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.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
Collections Dwight Deugo Nesa Matic
CSE 143 Lecture 11: Sets and Maps reading:
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.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Java Collections CHAPTER 3-February-2003
Fundamental of Java Programming
Using the Java Collection Libraries COMP 103 # T2
Chapter 19 Java Data Structures
Efficiency of in Binary Trees
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Introduction to Collections
Part of the Collections Framework
CS2110: Software Development Methods
Introduction to Collections
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Hashing in java.util
Web Design & Development Lecture 6
Presentation transcript:

CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]

CSC 142 P 2 What is a collection?  A number of things that are grouped together in some way, e.g.,  A grocery cart contains all of the items that a customer wants to buy  A video store contains videos possibly grouped by genre, alphabetical order,…  A dictionary lists words along with their definition  A class list contains student names  Different kinds, e.g. duplicates/no duplicates, ordered/non ordered  Java offers several classes to support the concept of collections

CSC 142 P 3 Some collections in Java  ArrayList: collection whose elements are in a specific order (given by the index)  Vector: same as ArrayList, but designed to be used safely in a multithreaded environment (synchronization).  HashMap: collection of key/value pairs (as in a dictionary). The HashMap uses a hashcode to store the items in the collection (makes the retrieve operation efficient).  Available in the package java.util

CSC 142 P 4 Typical ArrayList operations (1)  Items in an ArrayList are ordered by their index value (starts at 0) // append at the end of the list public boolean add(Object o) // remove the Object at location index public Object remove(int index) // insert at location index public boolean add(int index, Object o)  Class Object  The ultimate base class: any class instance is of type Object Object o = new Integer(5); // OK  An ArrayList accepts any type of object. It grows or shrinks as needed.

CSC 142 P 5 Typical ArrayList operations (2)  Getting an element from an ArrayList public Object get(int index)  always get an Object (what get returns)  To get back an instance with the actual type use a cast:  ArrayList l = new ArrayList(); l.add(new String("ABC")); String s = l.get(0); // Error String s = (String)l.get(0); // OK  Other common methods  public boolean contains(Object o) public int size() public boolean isEmpty() public Iterator iterator() // see next slide

CSC 142 P 6 Iterating through an ArrayList  Using the index value  for(int i=0; i<l.size(); i++){ Object o = l.get(i); // process o }  Using an iterator  Iterator i = l.iterator(); while(i.hasNext()){ Object o = i.next(); // process o }  An iterator works with any type of collection

CSC 142 P 7 The Collections class  A powerful class that contains many static methods to operate on many types of collections (e.g. to synchronize, to make a collection read only, to sort…)  e.g. to sort  // to sort items that can be compared public static void sort(List list) // Note: an ArrayList is a List // to sort items according to some supplied // comparator public static void sort(List list, Comparator c)

CSC 142 P 8 ArrayList example  Input and print a class list in alphabetical order  // l is the list of students ArrayList l = new ArrayList(); // Get the students' names String s; do{ s=JOptionPane.showInputDialog(null, "Student Name"); if (s!=null) l.add(s); }while(s!=null); // Sort the list in alphabetical order Collections.sort(l); // Print the list Iterator i = l.iterator(); while(i.hasNext()) System.out.println(i.next());

CSC 142 P 9 HashMap  Example: the IRS list of taxpayers can be thought of as a map. Two items  key: SSN  value: taxpayer's info (name, income…)  Typical HashMap operations  //put returns the old value or null if none public Object put(Object key, Object value) public Object get(Object key) public Object remove(Object key) public int size()  What is a hash? Store the values in a table. The location of a value in the table is determined by a function (the hash function) applied on the key.

CSC 142 P 10 HashMap example  A phone book  HashMap d = new HashMap(); // Create the phone book String r,s; do{ r=JOptionPane.showInputDialog(null,"Name"); s=JOptionPane.showInputDialog(null,"Number"); if (s!=null && r!=null) d.put(r,s); }while(s!=null && r!=null) // Use the phone book r=JOptionPane.showInputDialog(null,"Name"); System.out.println("The phone number of " +r+ " is " + d.get(r)); 

CSC 142 P 11 Other collections  HashSet  for an unordered collection with no duplicates  TreeMap  for an ordered map (the ordering is done on the keys)