Using the Java Collection Libraries COMP 103 # T2

Slides:



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

Concrete collections in Java library
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
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.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
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.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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 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.
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.
Big Java Chapter 16.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
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.
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.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
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.
Slides by Donald W. Smith
Sixth Lecture ArrayList Abstract Class and Interface
Collections.
Some Collections: BAGS, SETS, and STACKS
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Software Development Java Collections
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
A tree set Our SearchTree class is essentially a set.
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Welcome to CSE 143!.
structures and their relationships." - Linus Torvalds
Building Java Programs
structures and their relationships." - Linus Torvalds
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
CSE 143 Lecture 2 Collections and ArrayIntList
Welcome to CSE 143!.
Review: Classes, Inheritance, and Collections
Welcome to CSE 143! Go to pollev.com/cse143.
CS2110: Software Development Methods
CSE 1020: The Collection Framework
Introduction to Data Structure
Parsing JSON, Using Libraries, Java Collections, Generics
Web Design & Development Lecture 6
CS 240 – Advanced Programming Concepts
structures and their relationships." - Linus Torvalds
Introduction to Java Collection
Presentation transcript:

Using the Java Collection Libraries COMP 103 #2 2012 T2

Menu: Programming with libraries Collections Programming with Lists of objects (Needed for CrowdRenderer)

Course Structure First half: Programming with unstructured and linear collections Different Kinds of collections: Lists, Sets, Bags, Maps, Stacks, Queues, Priority Queues Implementing, using, sorting, searching Linear collections... Second half: Programming with Hierarchical collections Building, traversing Tree structured collections Re-implementing linear collections with binary search trees with partially ordered trees hash tables

Recurring Themes Good Design: Efficiency: Testing: Which choices should you make? Choosing appropriate implementations for collections Making the right choice the first time Efficiency: How fast is it? How much memory does it take? By analysis, and by benchmarking Testing: Does it work right?

Programming with Libraries Modern programs (especially GUI and network) are too big to build from scratch. ⇒ Have to reuse code written by other people Libraries are collections of code designed for reuse. Java has a huge collection of standard libraries…. Packages, which are collections of Classes and Interfaces The comp102 library has about 10 classes (UI, Trace, UIMouseListener, UIFileChooser, …) There are lots of other libraries as well Learning to use libraries is essential. Java API

Libraries for COMP 103 java.util Collection classes Other utility classes java.io Classes for input and output comp102 UI, etc We will use these libraries in almost every program We will use other libraries also, where appropriate.

Using Libraries Read the documentation to pick useful library             Using Libraries Read the documentation to pick useful library import the package or class into your program import java.util.*; import java.io.*; (or just the class you want:) import java.awt.BorderLayout; import javax.imageio.ImageIO; Read the documentation to identify how to use Constructors for making instances methods that you can call Interfaces that you can implement Use the classes as if they were part of your program Java API

Collections What is a Collection? An object that contains other objects What defines the collection type? Structure (no/linear/hierarchical) Constraints on access, modification, etc Mostly don’t care how it’s stored or manipulated inside: As long as it has the right behaviour, the inside doesn't matter.

“Standard” Collections Common ways of organising a collection of values: Collection Bag Graph Set Tree Map Stack List Queue What's the difference?

Collections: What’s the difference Different types of values Different structures No structure – just a collection of values Linear structure of values – the order matters Set of key-value pairs Hierarchical structures Grid/table …. Different constraints duplicates allowed/not allowed get, put, remove anywhere get, put, remove only at the ends, or only at the top, or … get, put, remove by position, or by value, or by key, or …

Abstract Data Types Set, Bag, Queue, List, Stack, Map, etc are an ADT is a type of data, described at an abstract level: Specifies the operations that can be done to an object of this type Specifies how it will behave. We don't care how it is implemented underneath though we will always need some concrete implementation of it.

Eg: Set ADT (simple version) Operations: Behaviour: add(value ), remove(value ), contains(value )→boolean Behaviour: A new set contains no values. A set will contain a value iff the value has been added to the set and it has not been removed since adding it. A set will not contain a value iff the value has never been added to the set, or it has been removed from the set and has not been added since it was removed.

Java Collections Library Defines interfaces ⇒ Abstract Data Types and classes: Collection interfaces extends Set List Queue implements SortedSet HashSet LinkedHashSet TreeSet ArrayList LinkedList PriorityQueue classes

Java Collections library Interfaces: Collection = Bag (most general) List = ordered collection Set = unordered, no duplicates Queue ordered collection, limited access (add at one end, remove from other) Map = key-value pairs (or mapping) … Specify the Types: Classes List classes: ArrayList, LinkedList, Vector Set classes: HashSet, TreeSet, EnumSet, LinkedHashSet,… Map classes: EnumMap, HashMap, TreeMap, LinkedHashMap, WeakHashMap, … … Implement the interfaces Each implementation has advantages and disadvantage.

Java Interfaces and ADT’s A Java Interface corresponds to an Abstract Data Type Specifies what methods can be called on objects of this type (specifies name, parameters and types, and type of return value) Behaviour of methods is only given in comments (but cannot be enforced) No constructors - can’t make an instance: new Set() No fields - doesn’t say how to store the data No method bodies. - doesn’t say how to perform the operations public interface Set { public void add(Object item); /*…description…*/ public void remove(Object item); /*…description…*/ public boolean contains(Object item); /*…description…*/ … // (plus lots more methods in the Java Set interface)

Using Java Collection Interfaces Your program can Declare a variable, parameter, or field of the interface type private List students; // a list of Students Create a collection object students= new ArrayList (); Call methods on that variable, parameter, or field students.add(new Student("James Noble", 30001234)) students.get(3).reportGrades() Problem: How do we specify the type of the values in the collection? This won’t work! why?

Parameterised Types. The structure and access discipline of a collection is the same, regardless of the type of value in it: A set of Strings, a set of Persons, a set of Shapes, a set of integers all behave the same way. ⇒ Only want one Interface for each kind of collection. (there is only one Set interface) Need to specify kind of values in a particular collection ⇒ The collection Interfaces (and classes) are parameterised: Interface has a type parameter When declaring a variable collection, you specify the type of the collection and the type of the elements of the collection

Parameterised Types. It’s a Set of something, as yet unspecified Interfaces may have type parameters (eg, type of the element): public interface Set <E> { public void add(E item); /*…description…*/ public void remove(E item); /*…description…*/ public boolean contains(E item); /*…description…*/ … // (lots more methods in the Java Set interface) When declaring variable, specify the actual type of element private Set <Person> friends; private List <Shape> drawing; Type of value in Collection Collection Type

Using Java Collection Interfaces Your program can Declare a variable, parameter, or field of the interface type private List <Student> students; // a list of Students Create a collection object students= new ArrayList <Student> (); Call methods on that variable, parameter, or field students.add(new Student("James Noble", 30001234)) students.get(3).reportGrades() Note: value type can't be a primitive type: int, double, boolean, etc; have to use the "wrappers": Integer, Double, Boolean, etc Can then put int, double, boolean into the List. Now this works! why?

Using ArrayList: methods Lists have many methods, including: size(): returns the number of items in the list add(item ): adds an item to the end of the list add(index, item ): inserts an item at index (moves later items up) set(index, item ): replaces the item at index with item get(index ): returns the item at position index contains(item ): true if the list contains an item that equals item indexOf(item ): returns the index of item, (-1 if item not present) remove(item ): removes an occurrence of item remove(index ): removes the item at position index (both move later items down) You can use the “for each” loop on an array list, as well as a for loop Read the documentation from the sidebar of course home page.

Using ArrayList private ArrayList <Student> students = new ArrayList <Student>(); : Student s = new Student("James Noble", 300012345) students.add(s); students.add(0, new Student("Stuart Marshall", 300054321)); for (Student st : students( System.out.println(st); for (int i = students.size()-1; i>=0; i--) System.out.println(students.get(i)); if (students.contains(current)){ file.println(current); students.remove(current); } Collections.shuffle(students); Collections.sort(students); The Collections class has useful static methods that operate on ArrayLists and other Collections