16-August-2002cse142-21-Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
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 Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
The Java Collections Package C. DeJong Fall 2001.
Chapter 19 Java Data Structures
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections 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.
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Computer Science 209 Software Development Java Collections.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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:
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
List Interface and Linked List Mrs. Furman March 25, 2010.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
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.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
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
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
Software Development Java Collections
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Java Collections Overview
Welcome to CSE 143! Go to pollev.com/cse143.
Collections Framework
Introduction to Java Collection
Presentation transcript:

16-August-2002cse Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1

16-August-2002cse Libraries © 2002 University of Washington2 Readings and References Reading Other References »The Java tutorial »

16-August-2002cse Libraries © 2002 University of Washington3 Java fundamentals Object oriented programming »classes and objects »interfaces and inheritance »constructors, methods, variables The Java language »types, expressions »control flow »exceptions Development tools »editors, compiler, Java virtual machine

16-August-2002cse Libraries © 2002 University of Washington4 Java class libraries and data structures ArrayLists »representative of the many Collection types Arrays »can hold primitive types directly Input and Output »we just looked at the tip of the iceberg with this package

16-August-2002cse Libraries © 2002 University of Washington5 There's more, much more... Essential Classes »Collections and Internationalization Advanced GUI Building »Swing, 2D Graphics, Sound, and JavaBeans Networking and Connectivity »JDBC, RMI, IDL, Servlets, and Security Packaging »JAR and the Extension Mechanism Advanced Language Topics »Java Native Interface and Reflection

16-August-2002cse Libraries © 2002 University of Washington6 Collection interface Interfaces, Implementations, and Algorithms From Thinking in Java, page 462 Collection

16-August-2002cse Libraries © 2002 University of Washington7 java.util.Collection Interface Collection is the root interface in the collection hierarchy »A collection represents a group of objects (the elements of the collection) »Some collections allow duplicate elements and others do not »Some collections are ordered and others are unordered

16-August-2002cse Libraries © 2002 University of Washington8 Collection interface methods Defines two fundamental methods »boolean add(Object o) »Iterator iterator() These two methods are enough to define the basic behavior of a collection An Iterator lets you step through the elements in the Collection without knowing the index

16-August-2002cse Libraries © 2002 University of Washington9 Iterator interface Iterator ListIterator

16-August-2002cse Libraries © 2002 University of Washington10 Iterator Interface Defines three fundamental methods »Object next() »boolean hasNext() »void remove() (optional operation) These three methods provide access to the contents of the collection An Iterator knows position within collection Each call to next() gets the next element from the collection

16-August-2002cse Libraries © 2002 University of Washington11 Iterator Position with next()

16-August-2002cse Libraries © 2002 University of Washington12 Example - SimpleCollection public void processCollection(Collection c) { System.out.println(c.getClass().getName()); for (int i=9; i >= 0; i--) { c.add(i + " * " + i + " = "+i*i); } Iterator iter = c.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); }

16-August-2002cse Libraries © 2002 University of Washington13 Comparable and Comparator interfaces ComparableComparator

16-August-2002cse Libraries © 2002 University of Washington14 Comparable and Comparator interfaces java.lang.Comparable »Imposes a total ordering on the objects of each class that implements it »This ordering is referred to as the class's natural ordering, and the class's compareTo() method is referred to as its natural comparison method java.util.Comparator »Defines a comparison function, which imposes a total ordering on some collection of objects »Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order »Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap)

16-August-2002cse Libraries © 2002 University of Washington15 Comparable Example public class Vehicle implements Comparable, Cloneable {... /** * Compares this object with the specified object for order, based on the * vehicle identification number. o the other Vehicle object a negative integer, zero, or a positive integer as this * object is less than, equal to, or greater than the specified object. */ public int compareTo(Object o) { Vehicle other = (Vehicle)o; if (this.vin < other.vin) { return -1; } else if (this.vin > other.vin) { return +1; } else { return 0; }

Comparator example import java.util.*; import hw6.*; public class CompareByRoute implements Comparator { /** * This method compares two TransitBus objects based on their route * number. This is the only method required by the Comparator interface. oA the first object to be compared. Must be a TransitBus. oB the second object to be compared. Must be a TransitBus. a negative integer, zero, or a positive integer as the route * number of the first argument is less than, equal to, or greater than * the route number of the second argument. */ public int compare(Object oA, Object oB) { int rA = ((TransitBus)oA).getRoute(); int rB = ((TransitBus)oB).getRoute(); if (rA < rB) { return -1; } else if (rA > rB) { return +1; } else { return 0; }

16-August-2002cse Libraries © 2002 University of Washington17 List and Set interfaces Set List Collection

16-August-2002cse Libraries © 2002 University of Washington18 List and Set public interface List extends Collection »An ordered collection (also known as a sequence) »User can store and access elements by their integer index and search for elements in the list »Lists typically allow duplicate elements public interface Set extends Collection »A collection that contains no duplicate elements and at most one null element »Models the mathematical set abstraction

16-August-2002cse Libraries © 2002 University of Washington19 Concrete classes that implement List ArrayList List LinkedList

16-August-2002cse Libraries © 2002 University of Washington20 ArrayList and LinkedList ArrayList »fast access to any element in the List by index »implemented with an array of Objects, ie, Object[] »automatically increases array size when needed »add at the end is fast, but add in the front requires copying the entire array to make room LinkedList »fast insert and delete at any point in a list »slow if you want to access elements by index

16-August-2002cse Libraries © 2002 University of Washington21 Concrete classes that implement Set HashSet Set TreeSet

16-August-2002cse Libraries © 2002 University of Washington22 HashSet and TreeSet HashSet »Like all Collections, a HashSet stores objects »This class offers constant time performance for the basic operations (add, remove, contains and size) »No guarantee as to the order of the elements TreeSet »Guarantees that the set will be sorted in ascending element order

16-August-2002cse Libraries © 2002 University of Washington23 Map interface Map

16-August-2002cse Libraries © 2002 University of Washington24 Map interface public interface Map A class that implements the Map interface provides an object that maps keys to values »a map cannot contain duplicate keys »each key can map to at most one value This is how the property files for the sliding ovals were implemented arg = (String)pMap.get("width"); if (arg != null) { width = Integer.parseInt(arg); }

16-August-2002cse Libraries © 2002 University of Washington25 Concrete classes that implement Map HashMapTreeMap Map

16-August-2002cse Libraries © 2002 University of Washington26 HashMap and TreeMap HashMap »provides constant-time performance for the basic operations (get and put) TreeMap »guarantees that the map will be in ascending key order »provides guaranteed log(n) time cost for the containsKey, get, put and remove operations

16-August-2002cse Libraries © 2002 University of Washington27 Swing package Provides all the components needed for building a graphical user interface »package javax.swing and related packages Can build nice looking applications to run on a variety of underlying systems »for example jEdit, BusView

Swing components JSplitPane JTree JTabbedPane JToolBar JMenuBar JComboBox JTextComponent

16-August-2002cse Libraries © 2002 University of Washington29 useful packages java.net »Classes for implementing networking applications java.io »System input and output through data streams, serialization and the file system java.awt »Basic classes for creating user interfaces and for painting graphics and images »User interface event classes (mouse, keyboard,…)

16-August-2002cse Libraries © 2002 University of Washington30 useful packages javax.accessibility »Defines a contract between user-interface components and an assistive technology that provides access to those components internationalization (java.util, java.text, …) »Messages, labels on GUI components, online help, sounds, colors, graphics, icons, dates, times, numbers, currencies, measurements, phone numbers, honorifics and personal titles, postal addresses, page layouts

16-August-2002cse Libraries © 2002 University of Washington31 useful packages java.sql »Classes for accessing and processing data in databases java.util.zip »classes for reading and writing the standard ZIP and GZIP file formats java.util.jar »classes for reading and writing the JAR (Java ARchive) file format, which is based on the standard ZIP file format with an optional manifest file