JAVA COLLECTIONS LIBRARY

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.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
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.
Chapter 19 Java Data Structures
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.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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 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.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
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.
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.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
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.
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.
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.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington COMP T2.
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.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Sixth Lecture ArrayList Abstract Class and Interface
Collections.
Some Collections: BAGS, SETS, and STACKS
Chapter 19 Java Data Structures
Software Development Java Collections
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
Implementing ArrayList Part 1
CSE 373: Data Structures and Algorithms
COMP 103 Maps, Stacks Thomas Kuehne 2016-T2 Lecture 5
Road Map CS Concepts Data Structures Java Language Java Collections
structures and their relationships." - Linus Torvalds
Java Collections Overview
structures and their relationships." - Linus Torvalds
Welcome to CSE 143! Go to pollev.com/cse143.
Collections Not in our text.
CS2110: Software Development Methods
CSE 1020: The Collection Framework
Parsing JSON, Using Libraries, Java Collections, Generics
structures and their relationships." - Linus Torvalds
Presentation transcript:

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

TODAY Administrivia: Libraries and code reuse 2 Libraries and code reuse Java’s Collections Library ArrayList (our first example) Interfaces and Classes Administrivia: No cap on foreign material in submitted assignments but only original parts will be marked Register for tutorials with myAllocator by 3.00pm Friday! (email Alex Potanin in case you have problems)

Programming with Libraries 3 Modern programs (with GUIs, databases, network access, etc.) are too big to build from scratch, so we have to reuse code written by other people.... Libraries contain code designed for reuse Java has a huge number of standard libraries... organised in packages, which are groups of Classes The ecs100 library has some very useful classes (e.g. UI) There are LOTS of other libraries as well Learning to use libraries is ESSENTIAL Java API

Libraries for COMP103 4 ecs100 Special classes for text/graphical input and output, especially for GUI java.util Collection classes, and other utility classes java.io Classes for input and output javax.swing Large libraries of classes for GUI jawa.awt programs We will use libraries in almost every program

Using Libraries Read the documentation to pick useful library 5 Read the documentation to pick useful library Import the package or class into your program import java.util.*; import ecs100.*; Read the documentation to identify how to use constructors for making instances methods that you can call interfaces that you can / need to implement Use the classes as if they were part of your program Java API

“Standard” Collections 6 Common ways of organizing a collection of values: Each of these constitutes a different type of collection Collection Bag Graph Tree Stack Queue List Map Set

Java Collections Library Standard collections (eg. Set, List, Map, etc.) are available to use Collections specified as interfaces Collection, Set, List, Queue – are all interfaces Using collections implies using one of the many concrete classes that implement these interfaces. E.g. You can use: ArrayList (a class) which implements List (an interface)

You have been using ArrayList 8 Part of the Java Collections framework 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 Need to specify the type of items Cannot use the [...] notation You have to call methods to access and assign

Using ArrayList: declaring 9 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; ArrayList private ArrayList <Student> students = new ArrayList <Student>(); Type of elements in the list is between “<” and “>” after ArrayList No initial size(!), No maximum size

TIP: Read Documentation from sidebar of course homepage Using ArrayList: methods 10 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 a regular for loop TIP: Read Documentation from sidebar of course homepage

Using ArrayList 11 private List <Student> students = new ArrayList <Student> (); Student s = new Student(“Davy Jones”, 300012345); 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(currentStudent)) { UI.println(currentStudent); students.remove(currentStudent); } .... ....

Collection An object that serves as a container for other objects 12 An object that serves as a container for other objects Type of Elements (a collection of ….) Structure (none/linear/hierarchical) Constraints (duplicates allowed? Access: add anywhere, remove from one end only, get from top, etc…)

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

Is ArrayList a Collection type then? 14 Type of Elements ArrayList of Students Structure linear order Constraints Duplicates allowed, add/remove anywhere actually no! ArrayList is not a Collection type. List is! ArrayList is just ONE WAY to implement a List

extends implements interface (a specification / promise)  what it is class (an implementation)  how it is implemented Interfaces are organised in a hierarchy; they “extend” one another

Java Collections Library 16 Interfaces… Collection most general: “a container” List ordered collection Set unordered, no duplicates Queue ordered collection with limited access (add at one end, remove from other) Map key-value pairs …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 abstract concrete

Java 8 adds “default” bodies, though Java Interfaces 17 At the simplest level: An interface is a bunch of method signatures with a name At a higher level: An interface is an abstract concept that defines: the operations that are supported by 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 Assertions & JML Java 8 adds “default” bodies, though

Declaring a List Earlier, we declared a list of students like this: 19 Earlier, we declared a list of students like this: private ArrayList <Student> students = new ArrayList <Student>(); BETTER / NOW: declare as an instance of the interface List: private List <Student> students = new ArrayList <Student>();

Summary Libraries Collections 20 Libraries Collections Interfaces – correspond to ADTs. No concrete details e.g., Collection, List, Set, … Classes – provide implementations for interfaces e.g., ArrayList  implements an interface (List) from the Java Collections Library