Title: LinkedLists
1LinkedLists
2What is a list?
- A list is an object that is a sequence of
objects. - The positions in the list are numbered 0, 1, 2,
or may be numbered 1, 2, 3, Examples45, 75,
31, 52, 27Bob, Nick, Anne, Mary, Larry, Celia
0
1
2
3
4
5
3LinkedList of Chairs
4LinkedList
Head
5What is a LinkedList?
- A LinkedList is one of the many ways to implement
a list - Each object is stored in a node of the form
next node
object
6Instantiating a LinkedList
- LinkedListltEgt
- Examples
- LinkedListltBigIntegergt bigLst new
LinkedListltBigIntegergt() - LinkedListltStringgt stringLst new
LinkedListltStringgt()
7LinkedList Functionality
- add(aObject) add aObject to end of list
add(new BigInteger(234512909345689131321122110863
523)) - add(aObject, position) add aObject at the
position - size() size of list
- contains(aObject) does aObject belong to list
- get(position) get object at position in list
- remove() remove object at beginning of list
- remove(position) remove object from position
- indexOf(aObject) - remove object at position
8LinkedListltStringgt list new LinkedListltStringgt()
list.add("Bob") list.add("Al") list.add("Carl
") list.add(1,"Sela") list.add(0,"Morgan") list
.add(3,"Mona") System.out.println("list "
list) System.out.println("size "
list.size()) if(list.contains("Mona"))
list.remove(list.indexOf("Mona"))
System.out.println("deleting "
"\"Mona\"") System.out.println("list "
list) System.out.println("first item in list is
" "\"" list.getFirst() "\"") System.out.pri
ntln("last item in list is " "\""
list.getLast() "\"")
9Maps
10What is a Map?
- An object that maps keys to values
- A map cannot contain duplicate keys
- A map may contain duplicate values
- Each key can map to at most one value.
231-45-3278
Jones, William
229-03-1259
Keller, Mary
256-41-0920
Madden, Matt
209-60-8877
Jones, William
values
keys
11HashMap
- Table consisting of two columns
- Column 1 contains the keys
- Column 2 contains the values
Keys
Values
231-45-3278
Jones, William
229-03-1259
Keller, Mary
256-41-0920
Madden, Matt
200-60-8877
Jones, William
Like a one-dimensional array, where a key acts
like an index to access the corresponding value
12Instantiating a HashMap
- HashMapltK,Egt
- Examples
- HashMapltString, Stringgt hmap new
HashMapltString, Stringgt() - HashMapltString, LinkedListltStringgtgt amap
new HashMapltString,
LinkedListltStringgtgt()
13HashMap Functionality
- get(aKey) returns corresponding value
get(229-03-1259) Keller, Mary - put(aKey, aValue) inserts a ltkey,valuegt pair
put(229-03-1259, Keller, Mary) - containsKey(aKey) does key belong
- containsValue(aValue) does value belong
- remove(aKey) remove a pair whose key is aKey
14HashMap Class
- values() returns the set of all values
- keySet() - returns the set of all keys
A HashMap is an implementation of a data
structure called a hash table
15import java.util. public class testHashMap
public static void main(String args)
HashMapltString,Doublegt table new
HashMapltString,Doublegt()
table.put("Bob",28.0) table.put("Al",28.0)
table.put("Carl",38.0)
System.out.println(table.get("Al"))
System.out.println(table.get("Carl"))
if(!table.containsKey("Al"))
table.put("Al",28.0) if(!table.containsKey(
"Donna")) table.put("Donna",24.0)
System.out.println(table.keySet())
System.out.println(table.values())
16TreeMap
- Sorted table with respect to keys
- Column 1 contains the keys
- Column 2 contains the values
Keys
Values
200-60-8877
Jones, William
229-03-1259
Keller, Mary
sorted
231-45-3278
Jones, William
256-41-0920
Madden, Matt
17Instantiating a TreeMap
- TreeMapltK,Egt
- Examples
- TreeMapltString, Stringgt hmap new
TreeMapltString, Stringgt() - TreeMapltString, Doublegt amap new
TreeMapltString, Doublegt()
18TreeMap Functionality
- get(aKey) returns corresponding value
get(229-03-1259) Keller, Mary - put(aKey, aValue) inserts a ltkey,valuegt pair
put(229-03-1259, Keller, Mary) - containsKey(aKey) does key belong
- containsValue(aValue) does value belong
- remove(aKey) remove a pair whose key is aKey
19TreeMap Class
- values() returns the set of all values
- keySet() - returns the set of all keys
20TreeMap as a Tree
- A TreeMap is an implementation of a data
structure called a binary search tree
231-45-3278
Jones, William
229-03-1259
Keller, Mary
256-41-0920
Madden, Matt
200-60-8877
Jones, William