bbm 102 introduction to programming iibbm102/slides2019/... · 2019-05-14 · 8 java collections...
TRANSCRIPT
![Page 1: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/1.jpg)
1
BBM 102 – Introduction toProgramming IISpring 2019
Collections Framework
![Page 2: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/2.jpg)
2
Today
The java.util.Arrays class
Java Collection Framework
java.util.Collection interface
java.util.List interface
java.util.ArrayList class
java.util.Set interface
java.util.HashSet class
java.util.Map interface
java.util.HashMap class
java.util.Hashtable class
java.util.Properties class
![Page 3: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/3.jpg)
3
java.util.Arrays
Provides high-level static methods for manipulating arrays, such as:
sort for sorting an array,
binarySearch for searching a sorted array,
equals for comparing arrays
fill for placing values into an array
![Page 4: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/4.jpg)
4
Arrays – Example
![Page 5: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/5.jpg)
5
Arrays – Example (continued)
![Page 6: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/6.jpg)
6
Arrays – Example (continued)
![Page 7: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/7.jpg)
7
Arrays – Example (continued)
![Page 8: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/8.jpg)
8
Java Collections Framework
Group of objects can be kept in an array, but arrays are not feasible when number of objects increase or decrease during the execution of the program
The Java Collections Framework is a collection of interfaces and classes that may be used to manipulate groups of objects
The classes implemented in the Java Collections Framework serve as reusable data structures and include algorithms for common tasks such as sorting or searching
The framework uses parameterized classes so you can use them with the classes of your choice
The framework is largely contained in the package java.util
![Page 9: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/9.jpg)
9
Basic collections
The three major collections in Java:
Set: A collection of elements that is guaranteed to contain no duplicates.
List: An ordered collection of elements, often accessed by integer indexes or by iteration.
Map: A collection of key/value pairs in which each key is associated with a corresponding value.
![Page 10: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/10.jpg)
10
java.util.Collection interface
It is the highest level of Java’s framework for collection classes
It describes the basic operations that all collection classes (except Map) should implement
The JDK does not provide any direct implementations of this interface: it provides implementations of more specific sub-interfaces like Set and List as in the example diagram below:
![Page 11: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/11.jpg)
11
What is a Set?
![Page 12: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/12.jpg)
12
java.util.Set interface
A Set is a Collection that contains unique elements (i.e., no duplicate elements).
The collections framework contains several Setimplementations, including HashSet
Major operations are:
Adding an element
Removing an element
Checking whether an element exists
Useful for answering questions of the form “have I seen this before?”
![Page 13: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/13.jpg)
13
![Page 14: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/14.jpg)
14
![Page 15: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/15.jpg)
15
![Page 16: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/16.jpg)
16
![Page 17: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/17.jpg)
17
![Page 18: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/18.jpg)
18
![Page 19: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/19.jpg)
19
Basic Set Operations
To insert an item:
set.add(value)
To check whether a value exists
set.contains(value)
To remove an item set.remove(value)
Union of two sets
set1.addAll(set2)
Intersection of two sets
set1.retainAll(set2)
Difference of two sets set1.removeAll(set2)
![Page 20: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/20.jpg)
20
HashSet - Example
![Page 21: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/21.jpg)
21
HashSet – Example (continued)
![Page 22: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/22.jpg)
22
What is a List?
![Page 23: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/23.jpg)
23
java.util.List interface
A List (sometimes called a sequence) is an ordered Collection
It can contain duplicate elements
Like array indices, List indices are zero based (i.e., the first element's index is zero)
In addition to the methods inherited from Collection, List provides methods for;
manipulating elements via their indices
manipulating a specified range of elements
searching for elements
getting a ListIterator to access the elements.
![Page 24: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/24.jpg)
24
Iterators
To visit every element of a collection, you can use the “for each” loop:
for(ElemType elem: collection){
…
}
Alternatively, you can use an iterator, an object whose job is to walk over the elements of a collection.
The iterator has two commands:
hasNext() returns true if there are more items to visit.
next() returns the next item and moves the iterator to the next position.
![Page 25: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/25.jpg)
25
ArrayList and Iterator - Example
![Page 26: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/26.jpg)
26
ArrayList and Iterator – Example (continued)
![Page 27: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/27.jpg)
27
ArrayList and Iterator - Example (continued)
![Page 28: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/28.jpg)
28
What is a Map?
![Page 29: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/29.jpg)
29
java.util.Map interface
Maps associate keys to values and cannot contain duplicate keys (i.e., each key can map to only one value)
Maps differ from Sets in that Maps contain keys and values, whereas Sets contain only values
![Page 30: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/30.jpg)
30
https://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html
![Page 31: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/31.jpg)
31
HashMap - Example
![Page 32: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/32.jpg)
32
HashMap – Example (continued)
![Page 33: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/33.jpg)
33
HashMap – Example (continued)
![Page 34: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/34.jpg)
34
What is a Hashtable?
![Page 35: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/35.jpg)
35
![Page 36: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/36.jpg)
36
![Page 37: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/37.jpg)
37
java.util.Properties class
A Properties object is a persistent Hashtable that
stores key-value pairs of strings
assumes that you use methods setProperty and getProperty to manipulate the table rather than inherited Hashtable methods put and get.
A common use of Properties objects is to maintain application-configuration data or user preferences for applications
![Page 38: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/38.jpg)
38
Properties - Example
![Page 39: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/39.jpg)
39
Properties – Example (continued)
![Page 40: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/40.jpg)
40
Properties – Example (continued)
![Page 41: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/41.jpg)
41
Properties – Example (continued)
![Page 42: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/42.jpg)
42
Properties – Example (continued)
![Page 43: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/43.jpg)
43
Properties – Example (continued)
![Page 44: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/44.jpg)
44
Summary
Arrays are used for a group of objects, but maintanence is difficult when the size of the group changes during the execution of the program
Java Collection Framework includes many interfaces and classes to easily manage groups of objects
The interfaces and classes of the framework are mostly in java.util package
Various interfaces include methods for special algorithms of various data structures
Framework includes various implementations of these interfaces
It is possible to implement or extend the interfaces/classes of the framework for new/different implementations of new/different data structures
![Page 45: BBM 102 Introduction to Programming IIbbm102/slides2019/... · 2019-05-14 · 8 Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when](https://reader030.vdocuments.net/reader030/viewer/2022040913/5e8902d64ef530113e7b9930/html5/thumbnails/45.jpg)
45
Acknowledgements
The course material used to prepare this presentation is mostly taken/adopted from the list below:
Java - How to Program, Paul Deitel and Harvey Deitel, Prentice Hall, 2012
Building Java Programs – A Back to Basics Approach, Stuart Reges and Marty Stepp, Addison Wesley, 2011
Stanford, Collections – lecture notes