1 of 31

Section 1

ADTs, Data Structures, Java Generics

2 of 31

Hello, I’am Mia

  • She/her
  • Incoming junior
  • I like cooking & swimming

  • First time being a TA!!

3 of 31

Hi! I’m Rachel

  • She/her
  • Incoming junior
  • Bellevue, WA
  • Hobbies:
    • Watching movies
    • Drawing
    • Nowadays: watching WC!
  • I have two cats

4 of 31

Icebreaker Activity!

  1. Introduce yourself:
    1. Name
    2. Pronouns
    3. Year/Major

  1. Question: What’s something you are looking forward to this summer?

5 of 31

Abstract Data Type (ADT)

From lecture:

Mathematical description of a “thing” with set of operations on that “thing”

Main Idea:

Based on the point of view from the USER (as opposed to the implementer)

3 Descriptors:

  1. Operation (e.g. stack has a push method operation)
  2. Behaviour (e.g. stack has LIFO behaviour)
  3. Data Type (e.g. stack can store any elements)

6 of 31

Data Structure

From lecture:

A specific organization of data and family of algorithms for implementing an ADT

Main Idea:

Based on the point of view from the IMPLEMENTER (as opposed to the user)

i.e. it is the specification (data + algorithms) of an ADT

e.g. stack can use an underlying array data structure and specific pop/push algorithms

7 of 31

List Abstract Data Type

Summary/Idea: An indexable collection of items held in a sequence

Identify List operations:

Identify List Data Structures:

8 of 31

List Abstract Data Type

Summary/Idea: An indexable collection of items held in a sequence

Identify List operations:

  • Get
  • Add
  • Remove

Identify List Data Structures:

  • Linked List
  • Array List

9 of 31

Stack Abstract Data Type

Summary/Idea: A collection of items accesses in LIFO order

Identify Stack Operations:

Identify Stack Data Structures:

10 of 31

Stack Abstract Data Type

Summary/Idea: A collection of items accesses in LIFO order

Identify Stack Operations:

  • Push
  • Pop
  • Peek

Identify Stack Data Structures:

  • Linked list
  • Array List (resizable array)

11 of 31

Queue Abstract Data Type

Summary/Idea: A collection of items accessed in FIFO order

Identify Queue Operations:

Identify Queue Data Structures:

12 of 31

Queue Abstract Data Type

Summary/Idea: A collection of items accessed in FIFO order

Identify Queue Operations:

  • Enqueue
  • Dequeue
  • Peek

Identify Queue Data Structures:

  • Linked List
  • Array List (resizable array)
  • Circular Array

13 of 31

Set Abstract Data Type

Summary/Idea: An unordered collection of items without duplicates

Identify Set Operations:

Identify Set Data Structures:

14 of 31

Set Abstract Data Type

Summary/Idea: An unordered collection of items without duplicates

Identify Set Operations:

  • Add
  • Remove
  • Contains

Identify Set Data Structures:

  • Linked List
  • Array List
  • Binary Tree
  • Hash Set

15 of 31

Pair ADT

Idea: One object which holds an ordered pair of items

Operations:

  • Set first
  • Get first
  • Set second
  • Get second

Data Structure:

  • An object with a first and second parameter

16 of 31

IntPair Data structure

public class IntPair {

private int first;

private int second;

public IntPair(int first, int second){

this.first = first;

this.second = second;

}

public int getFirst(){

return first;

}

public int getSecond(){

return second;

}

public void setFirst(int newFirst){

this.first = newFirst;

}

public void setSecond(int newSecond){

this.second = newSecond;

}

}

17 of 31

IntPair Data structure

public class IntPair {

private int first;

private int second;

public IntPair(int first, int second){

this.first = first;

this.second = second;

}

public int getFirst(){

return first;

}

public int getSecond(){

return second;

}

public void setFirst(int newFirst){

this.first = newFirst;

}

public void setSecond(int newSecond){

this.second = newSecond;

}

}

Potential restrictions?

Pair ADT

Idea: One object which holds an ordered pair of items

Operations:

  • Set first
  • Get first
  • Set second
  • Get second

Data Structure:

  • An object with a first and second parameter

18 of 31

IntPair Data structure

public class IntPair {

private int first;

private int second;

public IntPair(int first, int second){

this.first = first;

this.second = second;

}

public int getFirst(){

return first;

}

public int getSecond(){

return second;

}

public void setFirst(int newFirst){

this.first = newFirst;

}

public void setSecond(int newSecond){

this.second = newSecond;

}

}

This Pair data structure is set up to work exclusively with Integers. We cannot easily use this to hold pairs of doubles, strings, nodes, etc.

We would need to re-implement the class for each type!

19 of 31

Java Generics

  • Java is a strongly typed language, meaning everything needs a type
  • Java requires it know what type of things every collection holds
  • “Generics” is the name of the strategy Java uses to do this
    • Example: ArrayList<String> myArrayList = new ArrayList<>();
    • Name of the type that the array list contains goes inside <>
    • The ArrayList implementation is written to work for any kind of Object
  • Since we’re going to be implementing data structures this quarter, we need to know how to use these!
  • Note: different programming languages solve this problem in different ways, we’re just presenting on how Java does it.

20 of 31

Java Generics

  • Java is a strongly typed language, meaning everything needs a type
  • Java requires it know what type of things every collection holds
  • “Generics” is the name of the strategy Java uses to do this
  • Example:

ArrayList<String> myArrayList = new ArrayList<>();

  • Name of the type that the array list contains goes inside <>
    • The ArrayList implementation is written to work for any kind of Object

21 of 31

Java Generics

Compare to: Methods and parameters

public void myMethod(int amount)

  • amount is a placeholder for a value that is unknown until we call the method

public class myClass<T>

  • T is a placeholder for a type that is unknown until we initialize the class

Similar to how method parameters allow us to write code that works for many different values, generics allow us to write code that works for many different types.

22 of 31

Java Generics

  • Since we’re going to be implementing data structures this quarter, we need to know how to use Java Generics!
  • We will be practicing during the rest of this section…

Note: different programming languages solve this problem in different ways, we’re just presenting on how Java does it.

23 of 31

LikePair Data structure

public class LikePair<T> {

private T first;

private T second;

public LikePair(T first, T second){

this.first = first;

this.second = second;

}

public T getFirst(){

return first;

}

public T getSecond(){

return second;

}

public void setFirst(T newFirst){

this.first = newFirst;

}

public void setSecond(T newSecond){

this.second = newSecond;

}

}

Using generics we essentially treat the type itself as a field in the class.

We put <T> to indicate that the type that the pair will contain will be called “T”, and now we can use it just like we might use int or String!

24 of 31

UnlikePair Data structure

public class UnlikePair<A,B> {

private A first;

private B second;

public UnlikePair(T first, T second){

this.first = first;

this.second = second;

}

public T getFirst(){

return first;

}

public T getSecond(){

return second;

}

public void setFirst(T newFirst){

this.first = newFirst;

}

public void setSecond(T newSecond){

this.second = newSecond;

}

}

We can even have multiple types!

Here, the first item will be of type A, the second item will be of type B.

What do we need to update in the rest of the implementation?

25 of 31

UnlikePair Data structure

public class UnlikePair<A,B> {

private A first;

private B second;

public UnlikePair(A first, B second){

this.first = first;

this.second = second;

}

public A getFirst(){

return first;

}

public B getSecond(){

return second;

}

public void setFirst(A newFirst){

this.first = newFirst;

}

public void setSecond(B newSecond){

this.second = newSecond;

}

}

We can even have multiple types!

Here, the first item will be of type A, the second item will be of type B.

What do we need to update in the rest of the implementation?

  • Replace the Ts!

26 of 31

LikeAnimalPair Data structure

public class LikeAnimalPair<T extends Animal> {

private T first;

private T second;

public LikeAnimalPair(T first, T second){

this.first = first;

this.second = second;

}

public T getFirst(){

return first;

}

public T getSecond(){

return second;

}

public void setFirst(T newFirst){

this.first = newFirst;

}

public void setSecond(T newSecond){

this.second = newSecond;

}

public String toString(){

return "(" + first + "," + second+ ")";

}

}

Suppose we wanted to be able to assume that a type inherited from a particular class.

We can actually use the “extends” keyword to require a generic type to have a superclass!

In this example, both items need to be the same animal

27 of 31

1. TypeSubtypePair - page 1

The first item is of some type, the second item is of a subtype of the first

What should go here?

public class TypeSubtypePair < >{� private A first;� private B second;� public TypeSubtypePair(A first, B second){� this.first = first;� this.second = second;� }� public A getFirst(){� return first;� }� public B getSecond(){� return second;� }� public void setFirst(A newFirst){� this.first = newFirst;� }� public void setSecond(B newSecond){� this.second = newSecond;� }� public String toString(){� return "(" + first + "," + second+ ")";� }�}

28 of 31

1. TypeSubtypePair - page 1

The first item is of some type, the second item is of a subtype of the first

What should go here?

A, B extends A

B extends A indicates that A is a superclass of B, or that B is a subtype of A

public class TypeSubtypePair <A, B extends A>{� private A first;� private B second;� public TypeSubtypePair(A first, B second){� this.first = first;� this.second = second;� }� public A getFirst(){� return first;� }� public B getSecond(){� return second;� }� public void setFirst(A newFirst){� this.first = newFirst;� }� public void setSecond(B newSecond){� this.second = newSecond;� }� public String toString(){� return "(" + first + "," + second+ ")";� }�}

29 of 31

2. IntOtherPair - page 2

The first item is an integer, the second item is some other type.

30 of 31

2. IntOtherPair - page 2

Why not <int, T> ?

31 of 31

Your turn!

Implement the following pair data structures:

3. ItemPairPair

  • The first item is of some type, the second item is a LikePair of things whose type inherits the first item’s

4. ItemArrayPair

  • The first item is of some type, the second item is an array of objects of another type

5. ComparablePair (challenge!!!)

  • A pair of items such that both of them implement the comparable interface.
  • A comparable pair must itself be comparable!