Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Java Collections Framework

Similar presentations


Presentation on theme: "The Java Collections Framework"— Presentation transcript:

1 The Java Collections Framework
Chapter 4 The Java Collections Framework

2 Chap.4 Contents 4.-1 Overview 4.0 Discrete Math Models 4.1 Collections
4.1.1 Collection Classes 4.1.2 Storage Structures for Collection Classes 4.2 Outline of the Java Collections Framework 4.2.1 Abstract Classes 4.2.2 Parameterized types 4.2.3 The Collection Interface 4.2.4 The List Interface 4.2.5 The Set Interface 4.2.6 The Map Interface

3 A collection is an object that is composed of elements.
4.-1 Overview A collection is an object that is composed of elements. The elements can be either references to object (such as String) OR primitive values (such as int) String[ ] names = new String[5]; int[ ] numbers = new int[5]; names ref 1 2 3 4 5 string1 numbers int 1 2 3 4 5 string2

4 A collection class is a class in which each instance (object) is a collection of elements, and each element is a reference to an object. EX: ArrayList<String> names = new ArrayList<String>(5); Parameterized class specifies element type in angle bracket < >. EX: ArrayList<Integer> numbers = new ArrayList<Integer>(5); Conversion between primitive data and object: int i; Integer myInt; String s; myInt = i; i = myInt; s = myInt.toString();

5 Interface (class interface) interface I1{ String m1(); int m2(); } abstract class abstract class A1 implement I1{ int i; String m1(){return “m1”;} int m2 (); } Class class C1 extends A1{ int j; int m2(){return “”c1} } class C2 extends A1{ int k; int m2(){return “c2”} }

6 A design pattern is a problem (ex: collection iteration) that occurs frequently and the outline of a solution (ex: the iterator interface below). interface Iterator<Entry> { boolean hasNext(); Entry next(); void remove(); }

7 4.0 Discrete Math Models The three models will be covered: 1. Set
2. Map 3. List (Sequence)

8 Set Ex. { a, b, c} A set is an unordered collection of objects, called elements or members of the set.

9 Map Ex: { a->1, b->2, c->2 }
A map is a set of function mappings, each maps an element (called key) in a set (called domain) to an element (called value) in a set (called codomain).

10 List Ex. < a0, a1, a2> A list (sequence) is a function
from a subset of the set of integers, usually {0,1,2,…} (called “index”) to a set S. an denotes the function mapping of the integer n.

11 Discrete Structure (Discrete Math and data structure)
This course will use various data structures to implement the discrete models mentioned above. For example, “ArrayList” class uses array to implement list, while “LinkedList” class uses linked structure to implement the same list.

12 4.1 Collections

13

14 Example

15

16

17 To small? A larger array must be allocated and
the contents of the smaller array copied to the larger array. Example: double[ ] salaries = new double [1000]; if salaries is too small, First double [ ] newSalaries = new double [2000]; Then System.arraycopy (salaries, 0, newSalaries, 0, 1000); Finally salaries = newSalaries; ※Java API: java.lang.System arraycopy (Object src, int srcPos, Object dest, int destPos, int length)           Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

18

19

20 Example 假設 array salaries 有100個元素空間,現已佔用了位置0~79.
欲將 寫入index 41. 則原本在salaries[41]的元素 移動到salaries[42],後面依此類推

21 4.1.1 Collection Classes Better than arrays:
Instances of collection classes A collection class is a class whose instances are collections.

22 collection 中的元素必須為(references to) objects. Example:
a String object can be an element, 基本型別 (int, double, boolean, …) 不能作為 collection 的元素, 但是對於每種基本型別都有其對應的 class (叫 wrapper classes )可用 如: Integer, Double, Boolean, … Primitive types are not object, but for each primitive type, there is a corresponding class, called wrapper class.

23 4.1.2 Storage Structures for Collection Classes
E for element Another hierarchy Map is not shown here

24

25

26

27 Example Entry object

28 4.2 Outline of the Java Collections Framework

29 4.2.1 Abstract Classes

30

31

32

33

34 A few more details on the relationship
between: interfaces, abstract classes, and fully defined classes:

35 If a class implements some but not all of the methods in an interface,
then the class would have to be declared as an abstract class ─ and therefore cannot be instantiated. An interface can extend one or more other interfaces. Ex: public interface A extends B, C{ …. A class can extend at most one other class; by default, the Object class is the superclass of every class. Multiple inheritance is illegal in Java. A class can implement more than one interface. Ex: class D implements interface1, interface2{ ….

36 4.2.2 Parameterized types Starting with J2SE (Java 2 Platform Standard
Edition) version 1.5, a class’s element type is specified in angle brackets (< >), when an instance of the class is declared.

37

38

39

40

41 /*boxing : 將 ”值” 包裝成 ”物件” */

42

43 Method get() Returns a Double object

44 4.2.3 The Collection Interface

45

46

47

48

49

50

51

52 Example

53

54 Exercise

55 4.2.4 The List Interface

56

57

58

59

60

61

62 4.2.5 The Set Interface

63

64

65

66

67 4.2.6 The Map Interface

68

69

70

71


Download ppt "The Java Collections Framework"

Similar presentations


Ads by Google