Bài giảng Lập trình hướng đối tượng: Chương 5 - Châu Thị Bảo Hà

58 6 0
Bài giảng Lập trình hướng đối tượng: Chương 5 - Châu Thị Bảo Hà

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng Lập trình hướng đối tượng - Chương 5 trình bày những nội dung liên quan đến tập hợp trên java. Sau khi học xong chương này người học có thể: Phân biệt được tập hợp và mảng, phân biệt được các đặc trưng của các Collection interface, biết cách chọn loại tập hợp thích hợp để giải quyết bài toán. Mời các bạn tham khảo.

Chương TẬP HỢP TRÊN JAVA Mục tiêu • • • Phân biệt tập hợp mảng Phân biệt đặc trưng Collection interface Chọn loại tập hợp thích hợp để giải tốn Nội dung 5.1 Khái niệm tập hợp 5.2 So sánh tập hợp mảng 5.3 Các lớp tập hợp Java 5.4 Ứng dụng tập hợp lập trình 5.1 Khái niệm tập hợp • • • Tập hợp đối tượng có khả chứa đối tượng khác Các đối tượng tập hợp thuộc nhiều loại liệu khác Các thao tác thông thường tập hợp o o o o o o Thêm/Xoá đối tượng vào/ra tập hợp Kiểm tra đối tượng có tồn tai tập hợp hay khơng Lấy đối tượng từ tập hợp Duyệt đối tượng tập hợp Xố tồn tập hợp … 5.1 Khái niệm tập hợp Collections Framework • Collections Framework (từ Java 1.2) o o • Là kiến trúc hợp để biểu diễn thao tác loại tập hợp Giúp cho việc xử lý tập hợp độc lập với biểu diễn chi tiết bên chúng Một số lợi ích Collections Framework o o o o Giảm thời gian lập trình Tăng cường hiệu chương trình Dễ mở rộng collection Sử dụng lại mã chương trình 5.1 Khái niệm tập hợp Collections Framework • Collections Framework bao gồm: o o o Interfaces: Là interface thể tính chất kiểu collection khác List, Set, Map Implementations: Là lớp collection có sẵn cài đặt collection interfaces LinkedList, HashSet Algorithms: Là phương thức tĩnh để xử lý collection, ví dụ: xếp danh sách, tìm phần tử lớn 5.1 Khái niệm tập hợp Collection Map interface Interface gốc chứa phương thức chung cho tất Lưu trữ ánh xạ từ khóa đến giá trị loại collections Lưu trữ không theo thứ tự thêm vào, không cho phép trùng Collection Set List Map Queue SortedMap Theo chế FIFO hàng đợi ưu tiên SortedSet Các khóa thứ tự Lưu trữ theo thứ tự thêm vào Truy xuất theo mục (index) Lưu trữ phần tử theo thứ tự tăng Có thể trùng 5.1 Khái niệm tập hợp So sánh số interface List +add(E):boolean +remove(Object):boolean +get(int):E +indexOf(Object):int Collection +add(E):boolean +contains(Object):boolean +size():int +iterator():Iterator etc… +remove(Object):boolean +contains(Object):boolean SortedSet +size():int +iterator():Iterator etc… Set +add(E):boolean +remove(Object):boolean +add(E):boolean +contains(Object):boolean +remove(Object):boolean +size():int +contains(Object):boolean +iterator():Iterator +size():int +first():E +iterator():Iterator etc… +last():E etc… 5.1 Khái niệm tập hợp Các phương thức Collection interface 5.1 Khái niệm tập hợp Duyệt collection • • Các phần tử collection duyệt thông qua Iterator interface Các lớp cài đặt Collection cung cấp phương thức trả Iterator phần tử chúng Collection c; Iterator it = c.iterator(); 5.3 Các lớp tập hợp Java Lớp Arrays • Ví dụ: Output 5.3 Các lớp tập hợp Java Các lớp bao (wrapper classes) • Collection làm việc Object Những kiểu liệu như: byte, short, int, long, double, float, char, boolean đưa trực tiếp vào Collection mà phải thông qua lớp bao • • Các lớp bao: Byte, Short, Integer, Long, Double, Float, Char, Boolean Ví dụ: Integer intObject = new Integer(9); int value = intObject.intValue(); 5.3 Các lớp tập hợp Java Lựa chọn sử dụng Collection 1) Determine how you access values o Values are accessed by an integer position Use an ArrayList • o Values are accessed by a key that is not a part of the object • o Go to Step 2, then stop Use a Map It doesn’t matter Values are always accessed “in bulk”, by traversing the collection and doing something with each value 2) Determine the element types or key/value types o o For a List or Set, a single type For a Map, the key type and the value type 5.3 Các lớp tập hợp Java Lựa chọn sử dụng Collection 3) Determine whether element or key order matters o Elements or keys must be sorted • o Elements must be in the same order in which they were inserted • o Use a TreeSet or TreeMap Go to Step Your choice is now narrowed down to a LinkedList or an ArrayList It doesn’t matter • If you chose a Map in Step 1, use a HashMap and go to Step 5.3 Các lớp tập hợp Java Lựa chọn sử dụng Collection • 4) For a collection, determine which operations must be fast o Finding elements must be fast • o Adding and removing elements at the beginning or the middle must be fast • o Use a HashSet and go to Step Use a LinkedList You only insert at the end, or you collect so few elements that you aren’t concerned about speed • Use an ArrayList 5.3 Các lớp tập hợp Java Lựa chọn sử dụng Collection 5) For HashSet and Map, decide if you need to implement the equals and hashCode methods o If your elements not support them, you must implement them yourself 6) If you use a Tree, decide whether to supply a comparator o If your element class does not provide it, implement the Comparable interface for your element class 5.4 Ứng dụng tập hợp lập trình Bài tập Cài đặt xử lý Exception cần thiết cho phương thức LinkedList, Stack, Queue, Tree Viết chương trình cho phép nhập xâu ký tự từ bàn phím, sau hiển thị xâu theo thứ tự ngược lại (dùng Stack) Viết chương trình cho phép nhập danh sách sinh viên sau xếp danh sách theo thứ tự tăng dần (dùng ArrayList Collections.sort()) Viết chương trình hỗ trợ tra cứu từ điển đơn giản Chương trình lưu từ nghĩa từ Collection Map Mở rộng tập cách dùng file để lưu trữ từ Giải tốn ứng dụng mơn Cấu trúc liệu cách sử dụng Collections Framework Special Topic: Hash Functions • • Hashing can be used to find elements in a set data structure quickly, without making a linear search through all elements A hashCode method computes and returns an integer value: the hash code o o Should be likely to yield different hash codes Because hashing is so important, the Object class has a hashCode method that computes the hash code of any object x int hh == x.hashCode(); x.hashCode(); int Computing Hash Codes • To put objects of a given class into a HashSet or use the objects as keys in a HashMap, the class should override the default hashCode method • A good hashCode method should work such that different objects are likely to have different hash codes o o It should also be efficient A simple example for a String might be: int hh == 0; 0; int for (int (int ii == 0; 0; ii

Ngày đăng: 21/05/2021, 12:48

Mục lục

  • Slide 1

  • Mục tiêu

  • Nội dung

  • 5.1. Khái niệm về tập hợp

  • 5.1. Khái niệm về tập hợp Collections Framework

  • 5.1. Khái niệm về tập hợp Collections Framework

  • 5.1. Khái niệm về tập hợp Collection và Map interface

  • 5.1. Khái niệm về tập hợp So sánh một số interface

  • Slide 9

  • 5.1. Khái niệm về tập hợp Duyệt collection

  • 5.1. Khái niệm về tập hợp Duyệt collection

  • 5.1. Khái niệm về tập hợp Comparable<T> interface

  • 5.1. Khái niệm về tập hợp Comparable<T> interface

  • 5.1. Khái niệm về tập hợp Comparator<T> interface

  • 5.1. Khái niệm về tập hợp Comparator<T> interface

  • 5.1. Khái niệm về tập hợp List interface

  • 5.1. Khái niệm về tập hợp Set and SortedSet interface

  • 5.1. Khái niệm về tập hợp Queue interface

  • 5.1. Khái niệm về tập hợp Map interface

  • 5.1. Khái niệm về tập hợp Map interface

Tài liệu cùng người dùng

Tài liệu liên quan