0% found this document useful (0 votes)
2 views54 pages

Week2 1 Java

Uploaded by

mitlucy1209
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)
2 views54 pages

Week2 1 Java

Uploaded by

mitlucy1209
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

Java

ArrayList, HashMap
Memory
Let's Talk About Memory
• Random Access Memory (RAM)
• Access any value by index
• E ectively, a giant array
• All values in your program are stored here
ff
Let's Talk About Memory
• Operating System (OS) Stack Memory
controls memory ...

• On program start, OS <Used by another program>


<Our Program Memory>
allocates a section of
memory for our program <Our Program Memory>
<Our Program Memory>
• Gives access to a range of <Our Program Memory>
memory addresses/indices
<Our Program Memory>
<Our Program Memory>
<Our Program Memory>
<Our Program Memory>
<Our Program Memory>
<Our Program Memory>
<Our Program Memory>
<Used by another program>
...
Stack Memory
• Fixed section of memory used to store variables
and stack frames
• One continuous section of RAM

• LIFO - Last In First Out


• New values are added to the end of the stack
• Only frames at the end of the stack can be
removed
Heap Memory
• ArrayLists and HashMaps will be stored in heap memory

• Heap memory is dynamic

• We can "ask" the OS/JVM for more heap space as needed

• Heap memory can be anywhere in RAM

• Location is not important

• Location can change

• Use references to nd data

• Variables only store references to values in the heap


fi
ArrayList
Java - ArrayList
package week2;
• Similar to:
import [Link];

public class ArrayList1 {


public static int sum(ArrayList<Integer> arrIn) {
• List in Python

int out = 0;
for (int x=0; x<[Link](); x++) {
out += [Link](x);
Array in JavaScript
}
return out;
}


public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
Sequential data structure
[Link](10-x);
}
[Link](arr1);
ArrayList<Integer> arr2 = arr1;
• Order matters
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}
• Values indexed starting at 0
Java - ArrayList
package week2; • ArrayList is built-in with Java
import [Link];

public class ArrayList1 {


• However, it is not automatically
public static int sum(ArrayList<Integer> arrIn) { available
int out = 0;


for (int x=0; x<[Link](); x++) {

}
out += [Link](x); Unlike String, int, double, etc.
return out;
}


public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
Must import ArrayList

[Link](10-x);
}
[Link](arr1);
The ArrayList class is in the
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
[Link] package
int total = sum(arr1);

}
[Link]("total: " + total);
• Importing makes the class
}
available in your code
Java - ArrayList
package week2; • Use the "new" keyword to create a
import [Link]; new ArrayList
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) { • Must have <> which is a type
int out = 0;
for (int x=0; x<[Link](); x++) {
parameter list
out += [Link](x);
}
return out;
• Can also have <Integer> in this
} example

public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>(); Must have () which is an empty
for (int x=0; x<4; x++) {
[Link](10-x); argument list
}
[Link](arr1);
ArrayList<Integer> arr2 = arr1; • This calls the classes constructor
[Link](arr2);
int total = sum(arr1); method and returns an object
[Link]("total: " + total);

}
}
• Much more detail to come in week 4
Java - ArrayList
package week2;
• An ArrayList variable should
import [Link];
have a type parameter in <>
public class ArrayList1 {


public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
for (int x=0; x<[Link](); x++) {
This ArrayList has a type
}
out += [Link](x);
parameter of Integer
return out;


}

public static void main(String[] args) {


We say this is an "ArrayList
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
of Integers"

}
[Link](arr1);
ArrayList<Integer> arr2 = arr1; This ArrayList can only ever
store Integers
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}
Java - ArrayList
package week2;
• The type parameter has to
import [Link];
be a class
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
for (int x=0; x<[Link](); x++) {
out += [Link](x);

}
}
return out; • Class types start with capital
public static void main(String[] args) {
letters
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {

}
[Link](10-x);
• You cannot create an
[Link](arr1);
ArrayList<Integer> arr2 = arr1; ArrayList of ints
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}
Java - ArrayList
package week2; • int ≈ Integer
import [Link];
• double ≈ Double

public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) { boolean ≈ Boolean
int out = 0;
for (int x=0; x<[Link](); x++) {
out += [Link](x); • Use the class equivalents for our
}
return out;
primitive (starts with lowercase letter)
} types

public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>(); [In most cases] Java will automatically
for (int x=0; x<4; x++) {
[Link](10-x); convert between the two
}
[Link](arr1);
ArrayList<Integer> arr2 = arr1; • Conversion is called auto-boxing
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
• We'll always use the primitive types
} unless we must use the class equivalent
}
Java - ArrayList
package week2;
• Call the add method to insert a
import [Link];
value at the end of the
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) { ArrayList
int out = 0;
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}

public static void main(String[] args) {


• Call get with an index to
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
retrieve that value at that index
[Link](10-x);
}
[Link](arr1);
ArrayList<Integer> arr2 = arr1;
• Cannot use [index] to access
[Link](arr2);
int total = sum(arr1);
a value in an ArrayList
[Link]("total: " + total);
}
}
Mem
ory
Diag
ram
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}

public static void main(String[] args) {


ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}

• It all starts the same


• It will quickly become very di erent
ff
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}

public static void main(String[] args) {


ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}

• We create an ArrayList
• ArrayLists go in the heap!
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}

public static void main(String[] args) {


ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}

• When an ArrayList is created on the heap:


• Create 2 columns: One for indices, one for values
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}

public static void main(String[] args) {


ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}

• Value on the heap always get a memory address


• "0x" followed by a number (You can choose any numbers for your diagrams)
• This tells java where in memory it can nd the value
fi
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}

public static void main(String[] args) {


ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}

• When a variable "stores" a value that's on the heap, it only store a


reference to that value
• arr1 only stores instructors of how to nd the ArrayList in the heap
fi
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0
return out;
}

public static void main(String[] args) {


ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}

• Each time we add a value to an ArrayList, it is added to the next


index
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2); [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
}
}

• Printing an ArrayList will print all it's values in [ ] separated by


commas
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2); [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
}
}

• When a variable is assigned a value that is a reference, only the reference is assigned!
• There is no copy of the ArrayList created. Only 1 ArrayList exists in memory
• That ArrayList is referred to by the 2 variables that store its reference
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2); [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
}
}

• This is **assign-by-reference**
• Only the reference is assigned
Technically it's assign-by-value, but the value is a reference
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2); [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
[10, 9, 8, 7]
}
}

• arr2 refers to the same ArrayList as arr1 -- the only ArrayList in this
example
• Printing arr2 is the same as printing arr1
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
[10, 9, 8, 7]
}
}

• When a method is called that take an object on the heap as a parameter, only the reference is
passed into the stack frame
• This is **pass-by-reference**
Technically it's pass-by-value, but the value passed is a reference
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
}
[Link]("total: " + total); x 0
}

• When using the reference, the dot operator . means we follow the reference to the
object to which it refers
• [Link]() means - go to the ArrayList referred to by this reference and call it's size
method
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 10 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
}
[Link]("total: " + total); x 0
}

• [Link](x)
• Follow the reference
• Return the value stored at index x and add 10 to the out variable
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 10 19 27 34 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
}
[Link]("total: " + total); x 0 1 2 3 4
}

• When x is 4, x<[Link]() is false


• The loop ends and x is removed from memory
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total 34
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 10 19 27 34 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
}
[Link]("total: " + total); x 0 1 2 3 4
}

• Return the value of the out variable to the total variable


• The entire stack frame is removed from memory
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total 34
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 10 19 27 34 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
[Link]("total: " + total); x 0 1 2 3 4
} total: 34
}

• Print total
• End of program
HashMap
Java - HashMap
package week2; • Similar to:
import [Link];

public class HashMap1 {


• Dictionary in Python
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); • Object in JavaScript
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
• Key-Value Store
}
[Link](key);

for (Integer value : [Link]()) {


• Order does not matter
}
[Link](value);

for (String key : [Link]()) {


• Cannot have duplicate keys
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value);

}
}
}
• Used to associate keys with values
Java - HashMap
package week2;

import [Link];
• Must import before use
public class HashMap1 {
public static void main(String[] args) {


HashMap<String, Integer> bills = new HashMap<>();

[Link]("Allen", 17); Most types we use from


here onward need to be
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) { imported
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
• Only primitives and classes
}
[Link](value); in the [Link] package do
}
}
not need to be imported
Java - HashMap
package week2; • HashMaps have 2 type
import [Link]; parameters
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); • First is the type of the keys
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
• Second is the type of the values
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {


[Link](key);
}
for (Integer value : [Link]()) {
We say this is a:
[Link](value);
}
for (String key : [Link]()) { • HashMap from String to
int value = [Link](key);
[Link](key + "'s number is: "); Integer
[Link](value);

}
}
}
• Maps Strings to Integers
Java - HashMap
package week2;
• Add key-value pairs using
import [Link];
"put"
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>();

[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));
• Retrieve a value at a
[Link](bills);
for (String key : [Link]()) { particular key using "get"
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value);
}
}
}
Java - HashMap
package week2;

import [Link];
• for-each loop
public class HashMap1 {
public static void main(String[] args) {
• Or "enhanced" loop in the
HashMap<String, Integer> bills = new HashMap<>();
world of Java
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));


[Link](bills);
for (String key : [Link]()) {
[Link](key); Very similar to Python loops
}
for (Integer value : [Link]()) {
[Link](value); for (type variableName : dataStructure)
}


for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
Read: for variableName in
}
}
[Link](value);
dataStructure
}
Doesn't have to be a data structure.
Anything that can be iterated over will work
Java - HashMap
package week2; • keySet

import [Link];

public class HashMap1 {


Allows us iterate (loop) over the
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>();
keys
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
• values
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
• Allows us to iterate over the
[Link](key); values
}
for (Integer value : [Link]()) {
[Link](value);
}


for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
Common to iterate over the keys
}
[Link](value);
and access the values if you
}
}
need both
Memory Diagram
Coleman=0}

Coleman

“Coleman” 0
“Coleman”

Coleman 0
0

“Coleman”

0
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
}
}

• HashMaps go in the heap


• Only a reference to the HashMap is stored on
the stack
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17);
[Link]("Coleman", 0);
"Allen" 17
[Link]("What is Allen's number? "); "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
}
}

• HashMaps have columns for keys and


values
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17);
[Link]("Coleman", 0);
"Allen" 17
[Link]("What is Allen's number? "); "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}

• HashMap prints as a list of key-value pairs in


{ } separated by commas
• Equal sign = separates each key from it's value
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17);
[Link]("Coleman", 0);
"Allen" 17
[Link]("What is Allen's number? "); "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}

• Order does not matter in a HashMap!


• Notice how “Coleman" was printed before "Allen"
• No simple way to predict the order
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17);
[Link]("Coleman", 0);
"Allen" 17
[Link]("What is Allen's number? "); "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}

• In your memory diagrams, any order is


acceptable for credit
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17); "Allen" 17
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
key "Allen" "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}
Allen
• Iterating over the keySet stores each key
in the "key" variable and runs the body
of the loop for each key
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17); "Allen" 17
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
key "Allen" "Coleman" "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}
Allen
• Once we iterate over all the keys, the loops ends Coleman

• Note: If there are no key-value pairs in the


HashMap, the loop body will never execute
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17); "Allen" 17
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
key "Allen" "Coleman" "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
value 17
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}
Allen
• Iterating over the values only stores the Coleman
17
values in the iteration variable
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17); "Allen" 17
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
key "Allen" "Coleman" "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
value 17 0
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}
Allen
• Iterate until we run out of values Coleman
17
0
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17); "Allen" 17
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
key "Allen" "Coleman" "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
value 17 0
}
for (Integer value : [Link]()) {
[Link](value); key "Allen"
value 17
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}
Allen
• If we iterate over the keys and get the values, Coleman
17
we can access the key-value pairs 0
Allen's number is: 17
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17); "Allen" 17
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
key "Allen" "Coleman" "Coleman" 0
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
value 17 0
}
for (Integer value : [Link]()) {
[Link](value); key "Allen" "Coleman"
}
for (String key : [Link]()) { value 17 0
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
} What is Allen's number? 17
} {Coleman=0, Allen=17}
Allen
• Reach the end of main Coleman
17

• That's the end of the program 0


Allen's number is: 17
Coleman's number is: 0
Coleman=0}

Coleman

“Coleman” 0
“Coleman”

Coleman 0
0

“Coleman”

You might also like