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

● Collections ● Interfaces ● Implementations Collections ● A collection, also called a container, is an object that groups multiple elements into a single unit. ● Collections are used to store, retrieve and manipulate data. – A collection of cards, a mail folders, a telephone directory

pdf10 trang | Chia sẻ: candy98 | Lượt xem: 497 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Object-Oriented Programming - Lecture 9: 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 9: Collections Lê Hồng Phương, Nguyễn Việt Hùng, Hà Mỹ Linh phuonglh@gmail.com Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi 2012-2013 Object-Oriented Programming: Collections 2 Content ● Collections ● Interfaces ● Implementations 2012-2013 Object-Oriented Programming: Collections 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 and manipulate data. – A collection of cards, a mail folders, a telephone directory... ● 2012-2013 Object-Oriented Programming: Collections 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. 2012-2013 Object-Oriented Programming: Collections 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 2012-2013 Object-Oriented Programming: Collections 6 Interfaces ● The core collection interfaces encapsulate different types of collections. ● These interfaces allow collections to be manipulated independently of their implementations. Collection Set List SortedSet Queue Map SortedMap 2012-2013 Object-Oriented Programming: Collections 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. ● E can be of any object type: – Integer, Double, String... – Object 2012-2013 Object-Oriented Programming: Collections 8 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(); ...} 2012-2013 Object-Oriented Programming: Collections 9 Implementations ● Commonly used implementations: – HashSet, TreeSet – ArrayList, LinkedList – PriorityQueue – HashMap, TreeMap ● You should read the API Documentation of these classes to know their rich functionalities. – Packages: java.util.* 2012-2013 Object-Oriented Programming: Collections 10 Simple usage of List // create a list of odd numbers List odds = new ArrayList(); for (int i = 1; i < 50; i = i + 2) { odds.add(i); } // print the list: first way for (Integer i : odds) { System.out.println(i); } // print the list: second way Iterator iter = odds.iterator(); int i; while (iter.hasNext()) { i = iter.next(); System.out.println(i); } // print the list: third way for (int j = 0; j < odds.size(); j++) { i = odds.get(j); System.out.println(i); }
Tài liệu liên quan