Object-Oriented Programming - Lecture 7: Collections - Lê Hồng Phương

● Collections ● Interfaces ● Implementations ● Algorithms ● Exercises

pdf12 trang | Chia sẻ: candy98 | Lượt xem: 540 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Object-Oriented Programming - Lecture 7: Collections - Lê Hồng Phương, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Lecture 7: Collections Lê Hồng Phương phuonglh@gmail.com Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi August, 2012 2 Content ● Collections ● Interfaces ● Implementations ● Algorithms ● Exercises August, 2012 3 Collections ● A collection, also called a container, is an object that groups multiple elements into a single unit. ● Collections are used to store, retrieve, manipulate data. – A collection of cards, a mail folders, a telephone directory... August, 2012 4 A collection framework ● A collection framework is a unified architecture for representing and manipulating collections. ● Three components of a collection framework: – Interfaces: abstracts data types that represent collections – Implementations: concrete implementations of collection interfaces – Algorithms: methods that perform useful computations such as searching and sorting on objects implementing collection interfaces. August, 2012 5 Benefits of the Java collection framework ● Reduces programming efforts ● Increases program speed and quality ● Allows interoperability among unrelated APIs ● Reduces effort to learn and to use new APIs ● Reduces effort to design new APIs ● Fosters software reuse August, 2012 6 Interfaces ● The core collection interfaces encapsulate different types of collections. ● These interfaces allow collections to be manipulated independently of their implementations. August, 2012 7 Interfaces ● All the interfaces of the Java collection framework are generic: – public interface Collection ● We should specify the type E of objects contained in the collection. August, 2012 8 Interfaces ● Exercise 1. Open the JDK Documentation and explore all the methods provided by the interface of the Java Collection Framwork. ● Start link: August, 2012 9 Traversing collections ● Two ways to traverse collections: – With the for-each construct – By using Iterator objects ● For-each construct: – for (Object o : collection) ● do something ● Iterator objects: – Collection c; – Iterator iter = c.iterator(); – while (iter.hasNext()) { E e = iter.next(); ...} August, 2012 10 Implementations ● Commonly used implementations: – HashSet, TreeSet – ArrayList, LinkedList – PriorityQueue – HashMap, TreeMap ● Exercise 2. Browse the documentation of these classes to know provided methods. August, 2012 11 Algorithms ● To be continued in the next lecture. August, 2012 12 Exercises ● Exercise 3. Write a program that reads a text file into a list. Each element of the list is a line. – Print the lines to the console in the reserved order. – Enter an integer n from the keyboard and then print n random lines of the list to the console. ● Exercise 4. Write a program that reads a text file into a set. Each element of the set is word of the text.
Tài liệu liên quan