Escolar Documentos
Profissional Documentos
Cultura Documentos
List Set
SortedSet
Map
SortedMap
1
Danh sách (List)
• Tạo danh sách rỗng
– Danh sách đặc: new ArrayList()
– Danh sách liên kết: new LinkedList() ()
• Cần "import java.util.*;" ở đầu mỗi file.
• Thêm phần tử vào danh sách
– add(value) (thêm vào cuối) hay add(index, value)
• Lấy phần tử thứ n
– get(index)
• Kiểm tra nếu phần tử đã có trong danh sách
– contains(element)
• Xoá phần tử
– remove(index)choặc remove(element)
• Tổng phần tử
– size()
ArrayList Example
import java.util.*;
public class ListTest1 {
public static void main(String[] args) {
List entries = new ArrayList();
double d;
while((d = Math.random()) > 0.1) {
entries.add("Value: " + d);
}
String entry;
for(int i=0; i<entries.size(); i++) {
entry = (String)entries
(String)entries.get(i);
get(i);
System.out.println(entry);
}
}
}
2
LinkedList
LinkedList Example
import java.util.*;
public class ListTest2 {
public static void main(String[] args) {
List entries = new LinkedList();
double d;
while((d = Math.random()) > 0.1) {
entries.add("Value: " + d);
}
String entry;
for(int i=0; i < entries.size(); i++) {
entry = (String)entries
(String)entries.get(i);
get(i);
System.out.println(entry);
}
}
}
List example
import java.util.*;
public class Test2{
3
List Example
import java.util.*;
public class Test3{
boolean equals(Object o)
4
int hashCode()
Ánh xạ (Map)
Là tập hợp các cặp key/value
Thêm phần tử
– put(Object key
key, Object value);
Lấy phần tử
– Object get(Object key)
Xoá phần tử:
– remove(Object key);
Kiểm tra phần tử:
–bboolean
l containsKey(Object
t i K (Obj t kkey); )
– boolean containsValue(Object value);
Tổng phần tử:
– int size();
5
Map Example
import java.util.*;
public class Test4{
Generics http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
6
Generics – Tham số kiểu
public interface List<E> {
void add(E x);
Iterator<E> iterator();
i
}
public interface Iterator<E> {
E next();
boolean hasNext();
}
Cách thức hoạt động không phụ thuộc vào kiểu dữ liệu.
Sử dụng Có thể định nghĩa kiểu dữ liệu cụ thể cho từng
loại phương thức
7
java.util.Arrays
1. import java.util.*;
2. class SearchCollectionTest {
3. public static void main(String[] args) {
4. String [] str = {"Mark", "Ready", "Set", "Go"};
5.
6. System.out.println("Unsorted:");
7. for (String s : str)
8. System.out.print(s + " ");
9. System.out.println("\nGo = " +
10. Arrays.binarySearch(str, "Go"));
11. Arrays.sort(str);
12
12. System out println("Sorted
System.out.println( Sorted in natural order:");
order: );
13. for (String s : str) System.out.print(s + " ");
14. System.out.println("\nGo = " +
15. Arrays.binarySearch(str, "Go"));
16. }
17.}
java.util.Arrays
1. import java.util.*;
2. class SearchCollectionTest {
3. public static void main(String[] args) {
4. String [] str = {"Mark", "Ready", "Set", "Go"};
5.
6
6. System out println("Unsorted:");
System.out.println( Unsorted: );
7. for (String s : str)
8. System.out.print(s + " ");
9. System.out.println("\nGo = " +
10. Arrays.binarySearch(str, "Go"));
11. MyReverseSorter ms = new MyReverseSorter();
12. Arrays.sort(str, ms);
13. for (String s : str) System.out.print(s + " ");
14. System.out.println("\nGo = " +
15. Arrays.binarySearch(str, "Go"));
16. System.out.println("Go = " +
17
17. A
Arrays.binarySearch(str,
bi S h( t "G " ms));
"Go", ))
18. }
19. }
20. class MyReverseSorter implements Comparator<String> {
21. public int compare(String s1, String s2) {
22. return s2.compareTo(s1);
23. }
24. }