hướng dẫn Colection

9 199 1
hướng dẫn  Colection

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

Thông tin tài liệu

java.util Package Tóm tắt học: package Util chứa class tiện ích mà java xây dựng sẵn Date, Random, phần package class collections Collection kiểu liệu tập hợp cho phép lưu trữ thao tác với nhóm đối tượng Chú ý: Map interface độc lập không kế thừa từ Collection List 1.1 ArrayList Sử dụng cấu trúc mảng để lưu trữ phần tử, nhiên có đặc điểm khác mảng:  Không cần khai báo trước kiểu phần tử  Không cần xác định trước số lượng phần tử(kích thước mảng) - Hàm Khởi tạo • • • ArrayList() ArrayList(Collection c) ArrayList(int initialCapactity) - Các methods chính: o o o o o o o add(Object o) remove(Object o) get(int index) size() isEmpty() contains(Object o) clear() Example: public static void main(String[] args) { ArrayList al = new ArrayList(); Point p = new Point(1,1); Integer i = new Integer(2); Double d = new Double(3); al.add(p); al.add(i); al.add(d); //Cấu trúc for – each java: duyệt tất phần tử arrayList for (Object o:al){ System.out.println(o); } } Result: 1.0 1.0 3.0 BUILD SUCCESSFUL (total time: seconds) 1.2 Vector Hoàn toàn giống ArrayList chức năng, khác biệt method Vector Synchronized VD: Khi có nhiều thread add remove phần tử Vector thread làm việc đó, thread khác bị lock Khi không làm việc với thread nên sử dụng ArrayList tốc độ nhanh Vector 1.3 LinkedList Về cấu trúc, object LinkedList chứa liên kết đến object kế sau list Khi duyệt list, không truy nhập trực tiếp mà phải từ phần tử  chậm nhiều so với arrayList hay vector Tuy nhiên add hay remove phần tử nhanh thực dồn phần tử … Trong Java, dùng method get(n) để lấy phần tử thứ n LinkedList, nhiên chất java phải duyệt qua n-1 object khác trước tìm object n Ngoài method ArrayList, LinkedList có thêm method:  addFirst()  addLast()  getFirst()  getLast()  removeFirst()  removeLast()  peek() = getFirst()  poll() = getFirst() + removeFirst() Khi dùng ArrayList, dùng LinkedList? - Nếu chương trình thường xuyên phải truy nhập trực tiếp liệu list  sử - dụng ArrayList Nếu chương trình thường xuyên phải thêm xóa phần tử list(đặc biệt thêm xóa giữa) nên sử dụng LinkedList Set Cũng kiểu collection list Điểm khác biệt Set List Set không bao giờ có phần tử giống nhau, có nghĩa add lần object lần add thứ tác dụng VD: Point p = new Point(1,2); HashSet hs = new HashSet(); hs.add(p); hs.add(p);//Lần add tác dụng 2.1 HashSet Được implement Set interface Hàm khởi tạo:  HashSet()  HashSet(Collection c)  HashSet(int capactity) Các method chính:  add(Object o)  remove(Object o)  contains(Object o)  size()  isEmpty() Tuy nhiên Set hàm get để lấy phần tử trực tiếp Muốn duyệt phần tử Set, sử dụng trỏ iterator: VD: public static void main(String[] args) { HashSet h = new HashSet(); h.add(new Point(1,1)); h.add(1); h.add(2); Iterator iterator = h.iterator(); while(iterator.hasNext()){ Object o = iterator.next(); System.out.println(o); } } Kết quả: 1.0 1.0 Mặc dù point add vào trước duyệt lại xuất sau Nguyên nhân HashSet không xếp thứ tự phần tử add 2.2 LinkedHashSet Giống HashSet ngoại trừ thứ tự phần tử set thứ tự add vào public static void main(String[] args) { LinkedHashSet h = new LinkedHashSet(); h.add(new Point(1,1)); h.add(1); h.add(2); Iterator iterator = h.iterator(); while(iterator.hasNext()){ Object o = iterator.next(); System.out.println(o); } } Kết quả: 1.0 1.0 BUILD SUCCESSFUL (total time: seconds) 2.3 TreeSet Thứ tự phần tử TreeSet không phụ thuộc vào thứ tự lúc add mà phụ thuộc vào thứ tự object set(nếu object so sánh thứ tự được) Ví dụ, add vào set object kiểu String object theo thứ tự alphabet TreeSet ts = new TreeSet(); ts.add("b"); ts.add("a"); ts.add("c"); Iterator iterator = ts.iterator(); while(iterator.hasNext()){ Object o = iterator.next(); System.out.println(o); } Kết quả: a b c BUILD SUCCESSFUL (total time: seconds) Map Map kiểu liệu dạng từ điển, phần tử bao gồm key value 3.1 HashMap Hàm khởi tạo:  HashMap()  HashMap(Collection c)  HashMap(int capacity) Các methods chính:  put(Object key, Object value)  get(Object key)  remove(Object key)  containsKey(Object key)  containsValue(Object value)  size()  isEmpty Khi put phần tử có key trùng với phần tử khác tồn map phần tử thay phần tử HashTable giống HashMap ngoại trừ method HashTable synchronized.(Tương tự ArrayList Vector) 3.2 LinkedHashMap Là HashMap phần tử xếp thứ tự insert vào map 3.3 TreeMap Các phần tử TreeMap xếp theo thứ tự so sánh tự nhiên (natural order) key VD: TreeMap tm = new TreeMap(); tm.put(2, "b"); tm.put(1,"a"); System.out.println(tm.firstKey()); Kết quả: BUILD SUCCESSFUL (total time: seconds) Workshop 6: Question 1: Exchange money Create a array of String: MoneyArr USD;VND;17000 EUR;USD;1.2 USD;IDN;15789 USD;EUR;0.83 CAD;IDN;16869 Write an application to convert some money from one currency to another The program need to input CODE1(1st currency code), CODE2(2nd currency code), and the amount to exchange then print out the amount after converting The MoneyArr should be input by user The program would have class: - class Rate with fields: CODE1(String), CODE2(String), rate(double) - class Exchange o has an arraylist or vector to store Rate objects o has a method: public double convert(String code1, String code2, double amount) where as:  code1: 1st currency  code2: 2nd currency  amount: the amount of money need to exchange - class Main o read user’s info o display the amount of money after converting Example: Enter the first currency code: USD Enter the second currency Code: VND Enter the amount of first currency: 100 The amount after converted: 1700000 Hint: - Read MoneyArr - use StringTokenizer to get 1st currency code, 2nd currency code, rate number Question 2: Word Count Write a program to read a array of String file then show how often a word appears For example: String Array: learn java by example guide to advance java example of distributed in java Output: learn : java: by: example: guide: to : advance: of: distributed: in: Total words: 13 Hint: use HashMap or HashTable Question 3: Dictionary A simple English – Vietnamese dictionary is stored in a array of Stringt, that’s named dictionary apple: qua tao ball: qua bong cat : meo dog : cho elephant: voi fish: ca gift: mon qua home: nha … Write a class Dictionary with method lookup to search the meaning of a new word: String lookup(String word); This method return the meaning of the word In case the word is not in dictionary, return null The main method should receive the word from user and show the meaning until an empty string is input For example: Enter the word: cat Meaning: meo Enter the word: bear Meaning: not found Enter the word: Presss any key to continued… Hint: use HashMap or HashTable

Ngày đăng: 19/10/2017, 16:26

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

Tài liệu liên quan