Day15-Collections Framework
Day15-Collections Framework
Collections
Framework
CS 2100
DSA1
Briana B. Morrison
Announcements
Lab:
• ArrayList
Assignments:
• Assignment 4 Blackjack
Other:
• Quiz 2 next weekend
• Review on Thursday at 7pm in Olsson 005
slido.com #3491501
Review
• Abstract Classes
• Within an inheritance hierarchy, is-a relationship
• Class with a partial implementation
• May have abstract methods which can be overridden
• Interfaces
• Collection of abstract methods (unimplemented methods)
• Default methods (using default keyword) – as of Java 8
• Static methods – as of Java 8
• Class extends for inheritance, implements for interface
• Only 1 class after extends, 1+ after implements
slido.com #3491501 4
Review
slido.com #3491501 6
• Reusable OO software / code components
• Large set of classes developed by someone else
• Partially complete: we add to it to make a
complete working program
• A unified architecture, works together, seamless,
Frameworks common interface
• Examples:
• Java Collections Framework
• JUnit Testing Framework
• Java Swing (GUI Framework)
slido.com #3491501 7
Frameworks
• A Framework in Java is a skeleton of reusable
software
• Reference Classes, Utility Classes
• Abstract Classes
• Interfaces
• Organized/unified architecture
• Highly optimized
• Today we will introduce the Java Collections
Framework - tools for containing and
manipulating a group of objects (like ArrayList)
slido.com #3491501
Java Collections Framework
• Computers are great at performing operations on a large number of things
• Depending on what the program needs to do, certain kinds of containers
will lend themselves to more efficient programs
• JCF provides data structures (DS) for abstract data types (ADT)
• Three fundamental types of collections:
• List: Stores objects in order
• Set: Stores a unique set of objects
• Map: Stores key/value pairs
• Containers
• Choose the right tool to match your problem
slido.com #3491501 9
https://www.codejava.net/java-core/collections/overview-
11
of-java-collections-framework-api-uml-diagram
It is really:
• Common set of operations for "abstract" data structures
• List Interface: Operations for any kind of list
• Set Interface: Operations for any kind of set
• Map Interface: Operations for any kind of map
Collections • Set of useful concrete classes that we can use
• ArrayList, HashMap, TreeSet
Framework • Common set of operations for all Collections
• Collection Interface: Operations for any Collection object
• Collection Class: Contains static methods that can
process Collection and List objects
• Different implementations are optimized for various
cases (more to come)
slido.com #3491501 12
Collection
Interface
slido.com #3491501
List
• List containers keep items in order
• For example - when taking orders at a pizza restaurant,
it is very important that the orders are made and
delivered in the order they are received. Or customers
will get angry!
• A List can contain duplicates
• Because they are kept in order, you can access
elements in a list using a “place” in the list - we
say elements can be randomly accessed and this
means from the computer’s perspective. The
programmer can access any element in the list at
any time using the index.
• Lists are optimized for maintaining order of
elements
slido.com #3491501
Maintain order as items are added
• Like an array in concept, indexed by a
number
• Allows for positional access - .get(i)
• Standard for loop can be used
Lists • Two examples (which implement List)
• ArrayList – uses an array for memory storage
• LinkedList – uses a linked list (references)
between objects
• Use Java API documentation
15
List Interface
slido.com #3491501
• Based on arrays
• Can dynamically change size
• Only stores objects
• Uses Wrapper classes for
primitives (Integer for int, etc.)
ArrayList • Lots of handy functions from
Collections Framework
slido.com #3491501 17
• ArrayList is a class provided by Java for storing a
list
• Lists store things in order
• ArrayLists have many features that improve on
ArrayList •
Arrays
ArrayLists can change size!
• ArrayLists supports a generic type
• ArrayList<Product> catalog;
slido.com #3491501
ArrayList Example
slido.com #3491501 19
ArrayList Demo
slido.com #3491501 20
Arrays vs. ArrayList
Arrays ArrayLists
• fixed size • dynamic size
• [] • .get and .set
• .length (attribute) • .size() (method)
• ok for small stuff • usually better choice
21
Implementing a List using Array(List)
Operation Performance
create (empty)
insert at end
insert at beginning
remove at end
remove at beginning
find - true
find - false
slido.com #3491501 22
Review
A. In general, two “kinds” of things are stored in a Java class. What are they?
B. Why do we call variables at the top of a class ‘Instance Variables’?
C. What kind of visibility modifier does an instance variable usually have? Why?
D. What are “Class Variables” and how are they different from Instance Variables?
E. What does a constructor do?
F. What kind of visibility modifier does a constructor have?
G. Does the constructor have a return type?
H. What must the constructor always be named?
I. Can a class have more than one constructor?
J. How will Java tell the constructors apart?
K. Do you have to include a return statement in a method if the return type is void?
slido.com #3491501 24