java collection algorithms

7 66 0
java collection algorithms

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

Thông tin tài liệu

http://vietjack.com/java/index.jsp Copyright © vietjack.com Thuật tốn Collection Java Collection Framework định nghĩa số thuật tốn mà áp dụng tới Collection Map Những thuật toán định nghĩa phương thức static bên lớp Collection Một số phương thức ném ClassCastException, xảy cố gắng so sánh kiểu khơng tương thích, ném UnsupportedOperationException, xảy cố gắng sửa đổi Unmodifiable Collection Để hiểu sâu khái niệm trình bày chương này, mời bạn tham khảo loạt bài: Ví dụ Collection Java Dưới liệt kê phương thức định nghĩa thuật toán Collection Framework Java: STT Phương thức Miêu tả static int binarySearch(List list, Object value, Comparator c) Tìm kiếm value list xếp theo c Trả vị trí value list; trả -1 value khơng tìm thấy static int binarySearch(List list, Object value) Tìm kiếm value list List phải xếp thứ tự Trả vị trí value list; trả -1 khơng tìm thấy value static void copy(List list1, List list2) Sao chép phần tử list2 vào list1 static Enumeration enumeration(Collection c) Trả liệt kê qua c static void fill(List list, Object obj) http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com Gán obj tới phần tử list static int indexOfSubList(List list, List subList) Tìm kiếm list xuất subList Trả mục so khớp đầu tiên; trả -1 khơng có so khớp tìm thấy static int lastIndexOfSubList(List list, List subList) Tìm kiếm list xuất cuối subList Trả mục so khớp đầu tiên; trả -1 khơng có so khớp tìm thấy static ArrayList list(Enumeration enum) Trả ArrayList mà chứa phần tử enum static Object max(Collection c, Comparator comp) Trả phần tử tối đa c xác định comp 10 static Object max(Collection c) Trả phần tử tối đa c xác định thứ tự tự nhiên Collection không cần xếp theo thứ tự 11 static Object min(Collection c, Comparator comp) Trả phần tử tối thiểu c xác định comp Collection không cần xếp theo thứ tự 12 static Object min(Collection c) Trả phần tử tối thiểu c xác định thứ tự tự nhiên 13 static List nCopies(int num, Object obj) Trả num obj chứa list không đổi num phải lớn http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com 14 static boolean replaceAll(List list, Object old, Object new) Thay tất old new list Trả true có thay xảy Nếu không false 15 static void reverse(List list) Đảo ngược dãy list 16 static Comparator reverseOrder( ) Trả comparator đảo ngược 17 static void rotate(List list, int n) Quay list n vị trí tới bên phải Để quay sang bên trái, sử dụng giá trị âm cho n 18 static void shuffle(List list, Random r) Xáo trộn phần tử list sử dụng r nguồn số ngẫu nhiên 19 static void shuffle(List list) Xáo trộn phần tử list 20 static Set singleton(Object obj) Trả obj set không thay đổi Đây cách dễ dàng để biến đổi đối tượng đơn vào set 21 static List singletonList(Object obj) Trả obj list không thay đổi Đây cách dễ dàng để biến đổi đối tượng đơn vào list http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp 22 Copyright © vietjack.com static Map singletonMap(Object k, Object v) Trả cặp key/value (k/v) map không thay đổi Đây cách dễ dàng để biến đổi cặp key/value đơn vào map 23 static void sort(List list, Comparator comp) Xếp thứ tự phần tử list xác định comp 24 static void sort(List list) Xếp thứ tự phần tử list xác định thứ tự tự nhiên 25 static void swap(List list, int idx1, int idx2) Trao đổi phần tử list mục xác định idx1 idx2 26 static Collection synchronizedCollection(Collection c) Trả Collection an toàn luồng (thread-safe) c 27 static List synchronizedList(List list) Trả thread-safe list list 28 static Map synchronizedMap(Map m) Trả thread-safe map m 29 static Set synchronizedSet(Set s) Trả thread-safe set s 30 static SortedMap synchronizedSortedMap(SortedMap sm) Trả thread-safe sorted set sm 31 static SortedSet synchronizedSortedSet(SortedSet ss) http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com Trả thread-safe set ss 32 static Collection unmodifiableCollection(Collection c) Trả unmodifiable collection c 33 static List unmodifiableList(List list) Trả unmodifiable list list 34 static Map unmodifiableMap(Map m) Trả unmodifiable map m 35 static Set unmodifiableSet(Set s) Trả unmodifiable set s 36 static SortedMap unmodifiableSortedMap(SortedMap sm) Trả unmodifiable sorted map sm 37 static SortedSet unmodifiableSortedSet(SortedSet ss) Trả unmodifiable sorted set ss Ví dụ Ví dụ sau minh họa thuật toán đa dạng Collection Java: import java.util.*; public class AlgorithmsDemo { public static void main(String args[]) { // Create and initialize linked list LinkedList ll = new LinkedList(); ll.add(new Integer(-8)); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com ll.add(new Integer(20)); ll.add(new Integer(-20)); ll.add(new Integer(8)); // Create a reverse order comparator Comparator r = Collections.reverseOrder(); // Sort list by using the comparator Collections.sort(ll, r); // Get iterator Iterator li = ll.iterator(); System.out.print("List sorted in reverse: "); while(li.hasNext()){ System.out.print(li.next() + " "); } System.out.println(); Collections.shuffle(ll); // display randomized list li = ll.iterator(); System.out.print("List shuffled: "); while(li.hasNext()){ System.out.print(li.next() + " "); } System.out.println(); System.out.println("Minimum: " + Collections.min(ll)); System.out.println("Maximum: " + Collections.max(ll)); } } Nó cho kết sau: List sorted in reverse: 20 -8 -20 List shuffled: 20 -20 -8 Minimum: -20 Maximum: 20 http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp http://vietjack.com/ Trang chia sẻ học online miễn phí Copyright © vietjack.com Page ... phí Page http://vietjack.com /java/ index.jsp Copyright © vietjack.com Trả thread-safe set ss 32 static Collection unmodifiableCollection (Collection c) Trả unmodifiable collection c 33 static List... idx1, int idx2) Trao đổi phần tử list mục xác định idx1 idx2 26 static Collection synchronizedCollection (Collection c) Trả Collection an toàn luồng (thread-safe) c 27 static List synchronizedList(List... unmodifiable sorted set ss Ví dụ Ví dụ sau minh họa thuật toán đa dạng Collection Java: import java. util.*; public class AlgorithmsDemo { public static void main(String args[]) { // Create and

Ngày đăng: 03/12/2017, 00:03

Mục lục

  • Thuật toán Collection trong Java

    • Ví dụ

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

  • Đang cập nhật ...

Tài liệu liên quan