Professional Documents
Culture Documents
Ni dung
Kiu Collections Giao din
Thc thi
Cc thut ton Lp vt cha Set v Map
Kiu Collections
Mt collection, hay cn gi l container, l mt i tng dng nhm
thoi,
http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
Collection framework
Mt collection framework l mt kin trc thng nht
din cc collection.
Thc thi: cc thc thi c th ca cc giao din ca
collection.
Thut ton: Cc phng thc h tr nh sp xp, tm
Ni dung
Kiu Collections Giao din
Thc thi
Cc thut ton Lp vt cha Set v Map
Giao din
Cc giao din li ca collection ng gi nhiu kiu khc nhau ca
collection. Cc giao din ny cho php cc collection c thao tc mt cch c lp vi cc thc thi ca chng.
Collection Map
Set
List
Queue SortedMap
SortedSet
Giao din
Tt c cc giao din ca Java collection framework l:
Duyt collection
Hai cch duyt qua collection: Bng cu trc foreach
do something
Cc i tng Iterator Collection<E> c; Iterator<E> iter = c.iterator(); while (iter.hasNext()) { E e = iter.next(); ...} Bi 9 - Kiu d liu Collection
Ni dung
Kiu Collections Giao din
Thc thi
Cc thut ton Lp vt cha Set v Map
10
Thc thi
Cc thc thi thng dng ca collection HashSet, TreeSet
ArrayList, LinkedList
PriorityQueue HashMap, TreeMap Bn nn c ti liu v cc lp ny trong API Documentation ny bit
V d: List
// create a list of odd numbers List<Integer> odds = new ArrayList<Integer>(); 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<Integer> 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); Bi 9 - Kiu d liu Collection }
12
Ni dung
Kiu Collections Giao din
Thc thi
Cc thut ton Lp vt cha Set v Map
13
Cc thut ton
Cc thut ton a hnh trong Collection framework ca Java c
th ti s dng.
Tt c cc thut ton u c vit di dng phng thc
tnh.
i s u tin lun l Collection hot ng.
14
Cc thut ton
Cc thut ton gm:
Sp xp Xo trn X l d liu thng xuyn Tm kim
Hp nht
Vic tm kim cc gi tr cc i
15
Sp xp
Cc thut ton sp xp sp xp li danh sch cc phn t
16
Sp xp
Giao din Comparable quy nh quan h th t cho mt lp. Cc i tng ca mt lp c th c sp xp t ng nu
BigInteger, BigDouble.
String, Data, File, CollationKey
17
Sp xp
Nu ta c gng sp xp mt danh sch m cc phn t ca n
Tr li mt gi tr ln hn, nh hn hay bng 0 ty thuc i tng ang xt ln hn, nh hn hay bng vi i tng c so snh.
18
Bi 9 - Kiu d liu Collection
Sp xp
Example: Name.java, NameSort.java
public class NameSort { public static void main(String[] args) { Name nameArray[] = { new Name("John", "Smith"), new Name("Karl", "Ng"), new Name("Jeff", "Smith"), new Name("Tom", "Rich") }; List<Name> names = Arrays.asList(nameArray); Collections.sort(names); System.out.println(names); } }
19
Bi 9 - Kiu d liu Collection
Sp xp
Nu cc i tng m chng ta cn phi sp xp khng thc thi
20
Sp xp
Gi s ta c mt lp Employee nh sau:
public class Employee implements public Name name() public int number() { ... { ...
21
Sp xp
ng nhin, chng ti c th sp xp danh sch cc nhn
vin theo tn ca h.
Nu cn chng ta cng c th sp xp danh sch ny theo th
t v thm nin?
22
Sp xp
public class EmpSort { static final Comparator<Employee> SENIORITY_ORDER = new Comparator<Employee>() { public int compare(Employee e1, Employee e2) { int dateCmp = e2.hireDate().compareTo(e1.hireDate()); if (dateCmp != 0) return dateCmp; return (e1.number() < e2.number() ? -1 : (e1.number() == e2.number() ? 0 : 1)); } Ta so snh cc gi tr numbers khi m hai gi }; tr ngy thng bng nhau // Employee database static final Collection<Employee> employees = ... ; public static void main(String[] args) { List<Employee> e = new ArrayList<Employee>(employees); Collections.sort(e, SENIORITY_ORDER); System.out.println(e); } }
Bi 9 - Kiu d liu Collection
23
Xo trn
Thut ton xo trn l ngc li ca thut ton sp xp
Sp xp li mt danh sch da trn u vo t mt ngun ngu
...)
24
Xo trn
public class Shuffle { public static void main(String[] args) { List<String> list = new ArrayList<String>(); for (String a : args) list.add(a); Collections.shuffle(list, new Random()); System.out.println(list); } }
public class Shuffle { public static void main(String[] args){ List<String> list = Arrays.asList(args); Collections.shuffle(list); System.out.println(list); Dng mt ngun ngu } nhin mc nh. }
25
Bi 9 - Kiu d liu Collection
Thao tc d liu
Collection cung cp 5 thut ton thc hin cc thao tc d
26
Tm kim
Thut ton binarySearch tm kim mt phn t trong
nu khng mt gi tr ch s m c tr li.
Hai dng ca thut ton:
Cho mt danh sch v mt phn t tm kim; Cho mt danh sch, mt phn t tm kim v mt
Comparator.
27
Hp nht
Thut ton v tn sut:
m s ln xut hin ca mt phn t trong mt collection.
28
Tm cc i
Gi tr cc i:
min ca mt collection max ca mt collection
Hai dng:
Cho mt Collection Cho mt Collection v mt Comparator
v d c th.
29
Bi 9 - Kiu d liu Collection
30
31
32
Fruit pineappale = new Fruit apple = new Fruit("Apple",100); Fruit orange = new Fruit("Orange",80); fruits[0]=pineappale; fruits[1]=apple; fruits[2]=orange; Arrays.sort(fruits); showList(fruits); } } 33} Bi 9 - Kiu d liu Collection
34
}
}; }
35
fruits 1 : Apple, Quantity : 100 fruits 2 : Orange, Quantity : 80 fruits 3 : Pineapple, Quantity : 70
36
}
Ni dung
Kiu Collections Giao din
Thc thi
Cc thut ton Lp vt cha Set v Map
37
Tm cc ch trong vn bn
Cc ch (token, word) l mt dy k t lin nhau, ngn cch bi k t
trng.
V d:
Dng Tom likes Jerry gm 3 ch: Tom, likes, Jerry. Dng Hc sinh hc sinh hc gm 5 ch:
C 3 ch khc nhau l Hc, sinh, hc.
Nu khng phn bit hoa thng th ch c 2 ch khc nhau.
38
Tm cc ch trong vn bn
Cho trc mt tp vn bn ting Anh hoc ting Vit, ta xt hai bi ton
sau:
Lit k cc ch khc nhau trong vn bn m tn s cc ch khc nhau trong vn bn
39
Tm cc ch trong vn bn
gii bi ton 1, ta dng kiu vt cha tp hp:
Giao din Set Ci t ca Set l HashSet
Ci t ca Map l HashMap
40
Set
List
Queue SortedMap
SortedSet
41
c tp vn bn
Reader reader = null; BufferedReader bufferedReader = null; try { // open a reader using UTF-8 encoding so that we can // read Vietnamese text reader = new InputStreamReader(new S dng bng m UTF-8 FileInputStream(fileName), "UTF-8"); c tp bufferedReader = new BufferedReader(reader);
42
c tp vn bn
String line = null; // read the input file, line by line while ((line = bufferedReader.readLine()) != null) { // split the line into tokens String[] tokens = line.split(Constants.DELIMITERS); // add tokens to the set for (String token : tokens) { tokenSet.add(token); Biu thc chnh quy ch nh cc k t } trng, du phy, du chm, m ngoc, }
ng ngoc
43
S dng Set
private Set<String> tokenSet; tokenSet = new HashSet<String>(); public void print() { System.out.println("There are " + tokenSet.size() + " different tokens."); for (String token : tokenSet) { System.out.println(token); } }
44
S dng Map
Mi nh x c cp phn t: Key Value Trong bi ton 2, Key l token, Value l mt s nguyn m s ln token xut hin Key, Value u phi l cc ki u i tng (khng th l kiu d liu c s)
45
S dng Map
// add tokens to the counter for (String token : tokens) { Integer count = tokenCounter.get(token); if (count != null) { tokenCounter.put(token, count + 1); } else { tokenCounter.put(token, 1); } }
46
S dng Map
/** * Prints the token counter to the standard console. */ public void print() { System.out.println("There are " + tokenCounter.keySet().size() + " different tokens."); for (String token : tokenCounter.keySet()) { System.out.println(token + " --> " + tokenCounter.get(token)); } }
47