0% found this document useful (0 votes)
16 views22 pages

Day15-Collections Framework

The document provides information about an upcoming lab, assignments, and review topics for a CS class. It includes the following announcements: 1. An upcoming lab will cover ArrayLists. 2. Assignment 4 is a Blackjack game. 3. There will be a Quiz 2 next weekend and a review session on Thursday at 7pm in Olsson 005. The document also provides a review of abstract classes, interfaces, and the Java Collections Framework which includes common operations for containers like lists, sets and maps and concrete classes like ArrayList.

Uploaded by

spxxdy2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views22 pages

Day15-Collections Framework

The document provides information about an upcoming lab, assignments, and review topics for a CS class. It includes the following announcements: 1. An upcoming lab will cover ArrayLists. 2. Assignment 4 is a Blackjack game. 3. There will be a Quiz 2 next weekend and a review session on Thursday at 7pm in Olsson 005. The document also provides a review of abstract classes, interfaces, and the Java Collections Framework which includes common operations for containers like lists, sets and maps and concrete classes like ArrayList.

Uploaded by

spxxdy2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Slido

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

Interfaces are *not* classes:


• Cannot instantiate an interface
• Not allowed: MyInterface i = new MyInterface(…);
• Does not contain any constructors
• Methods must be abstract, default, or static
• Cannot contain instance variables
• Any data fields must be both static and final
slido.com #3491501 5
Review
• A class can only inherit from one parent
• A class can implement more than one interface

• Implementing an interface forms a contract that is


enforced by the compiler

• Abstract parent class defines *what a class is*, an interface


defines *what it can do*
• If an abstract class only contains abstract methods,
create an interface instead

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

ArrayList<Integer> arrlist = new ArrayList<Integer>();


arrlist.add(5);
arrlist.add(12);
arrlist.add(1, 99);
System.out.println(arrlist.size());
System.out.println(arrlist);
if (arrlist.contains(99)) {System.out.println("99 there");}
else {System.out.println("99 not there");}

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

You might also like