JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2013-T2, Lecture 2 Marcus Frean.

Slides:



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

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
Java Collections Framework A presentation by Eric Fabricant.
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
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.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 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.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
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.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
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.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Collections Dwight Deugo Nesa Matic
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.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
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.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Using the Java Collection Libraries COMP 103 # T2
Sixth Lecture ArrayList Abstract Class and Interface
Some Collections: BAGS, SETS, and STACKS
Chapter 19 Java Data Structures
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Implementing ArrayList Part 1
Welcome to CSE 143! Go to pollev.com/cse143.
Presentation transcript:

JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean

TODAY 2  Libraries, Collections : Java Collections Library  Interfaces and Classes  Abstract Data Types  ArrayList (used in assignment#1) Administrivia:  this lecture repeats at 11am in Hunter  tutorial slots – please sign up a.s.a.p. here:

3 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 contain code designed for reuse  Java has a huge number of standard libraries... Packages, which are groups of Classes  The comp102 library has some very useful classes (eg. the UI Class)  There are LOTS of other libraries as well  Learning to use libraries is ESSENTIAL Java API

4 Libraries for COMP103  Comp102Special UI class for text/graphical input and output  java.utilthe Collection classes, other utility classes  java.ioClasses for input and output  javax.swingLarge library of classes for GUI jawa.awt programs We will use libraries in almost every program

 Read the documentation to pick useful library  Import the package or class into your program import java.util.*; import java.io.*; import comp102.*;  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 5 Using Libraries Java API

6 Collections  An object that serves as a repository for other objects (like a “container”)  Type of Elements (a collection of ….)  Structure (no/linear/hierarchical)  Constraints (duplicates ? Access: add anywhere, remove from one end only, get from top, etc…) Don’t care how it’s stored or manipulated inside

7 Example  Bag : a example of a collection  Type of Elements Bag of Students  Structure No structure/order  Constraints Duplicates allowed, add/remove anywhere (no order) Collection type Element type

8 “Standard” Collections  Common ways of organizing a collection of values:  Each of these is a different type of collection Collections BagGraphTreeStackQueueListMapSet

Java Collections Library  Standard collections (eg. Bag, Set, List, Map, etc.) are implemented for you!  Java Collections Library  Code written by others – for you to use!  Collections implemented as interfaces Collection, Set, List, Queue – are all interfaces Using collections implies using one of the many concrete classes that implement these interfaces. Eg. You can use: ArrayList (a class) which implements List (an interface). 9

Interfaces, hmm…. extends implements an interface: says what you’re doing an implementation (=a class): how you’re doing it And there’s a hierarchy to interfaces, so we say they “extend” one another

Java Interfaces 11  At the simplest level:  An interface is a bunch of method signatures with a name. (Method signature only, no bodies)  At a higher level:  An interface is an abstract concept that defines: the operations that can be done to an object of this type how it will behave  No concrete details!  No constructors- can’t make an instance  No fields- doesn’t say how to store the data  No method bodies.- doesn’t say how to perform the operations  Details provided by Classes that implement the Interface

12 Abstract Data Types (ADT)  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 Outlines how it will behave  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( ??? item); /*…description…*/ public void remove( ??? item); /*…description…*/ public boolean contains( ??? item);/*…description…*/ … // (plus lots more methods in the Java Set interface)

Interfaces :  Collection most general: “a bag”  List ordered collection  Set unordered, no duplicates  Queue ordered collection with 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 Java Collections Library 13 abstract concrete advantages and disadvantages

14 ArrayList (used lots in Assignment1)  Part of the Java Collections framework  Predefined class, implements List interface  Stores a LIST of items a collection of items kept in a particular order  Part of the java.util package ⇒ need import java.util.*; at the head of your file  You can make a new ArrayList object, and put items in it  Don’t have to specify its size: Like an infinitely stretchable array  Should specify the type of items  But, you can’t use the [...] notation  You have to call methods to access and assign

List of students (assume we have a class called Student )  Array: private static final int maxStudents = 1000; private Student[ ] students = new Student[maxStudents]; private int count = 0;  How would it look as an ArrayList instead? 15 Using ArrayList: declaring

 ArrayList: private ArrayList students = new ArrayList ();  better still: declare it as an instance of the interface List: private List students = new ArrayList ();  Type of elements in the list is between “ ” after ArrayList  No maximum, no initial size (!), no explicit count 16 Using ArrayList: declaring

17 Using ArrayList: methods ArrayList has many methods!  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  contains(item): true if the list contains an item that equals item  get(index): returns the item at position index  remove(item): removes an occurrence of item  remove(index): removes the item at position index (both “removes” move the later items down)  You can use the “for each” loop on an ArrayList, as well as for loop TIP: Read Documentation from sidebar of course homepage

18 Using ArrayList private List students = new ArrayList (); Student s = new Student(“Davy Jones”, ); students.add(s); for(Student st: students) UI.println(st.toString()); for(int i = students.size()-1; i>=0; i--) UI.println(students.get(i).toString()); if(students.contains(current)) { UI.println(current); students.remove(current); }....

 Libraries  Collections  Interfaces – correspond to ADTs. No concrete details.  Eg. Collection, List, Set…  ArrayList – example of a class that implements an interface (List) from the Java Collections Library. Summary 19

 How do we define the type of element a collection contains ?  Constructing a new object of an appropriate collection class.  What can we do with them ?  What methods can we call on them ?  How do we iterate down all the elements of a collection ?  How do we choose the right collection interface and class ?  What if there isn’t the right class for what we want ? What’s Next ? 20