27
1 Chương 10: Chương 10: Collection Collection GVLT: Trn Anh Dũng

c10 Collection 0766

Embed Size (px)

Citation preview

  • 1Chng 10: Chng 10: CollectionCollection

    GVLT: Trn Anh Dng

  • 2Ni dungNi dung

    Cu trc d liu trong Java Array LinkedList Stack v Queue

    Collections Framework Danh sch (List) Tp hp (Set) Bng nh x (Map)

  • 3Linked ListLinked List

    Linked list l cu trc gm cc node lin kt vi nhauthng qua cc mi lin kt. Node cui linked list ct l null nh du kt thc danh sch.

    Linked list gip tit kim b nh so vi mng trong ccbi ton x l danh sch.

    Khi chn/xo mt node trn linked list, khng phidn/dn cc phn t nh trn mng.

    Vic truy nhp trn linked list lun phi tun t.

  • 4Linked ListLinked List

    Th hin Node thng qua lp t tham chiu (self-referential class)

    Mt linked list c qun l bi tham chiu ti node uv node cui (firstNode & lastNode).

    class Node {private int data;private Node nextNode;// constructors and methods...

    }

    class Node {private int data;private Node nextNode;// constructors and methods...

    }15 10

  • 5Ci t Linked ListCi t Linked List

    // Dinh nghia mot node trong linked list class ListNode {

    int data;ListNode nextNode;ListNode(int value){

    this(value, null); }ListNode(int value, ListNode node){

    data = value; nextNode = node;

    }int getData(){ return data; }ListNode getNext() { return nextNode; }

    }

  • 6Ci t Linked ListCi t Linked List

    // Dinh nghia lop LinkedListpublic class LinkedList {

    private ListNode firstNode;private ListNode lastNode;public LinkedList(){

    firstNode = lastNode = null;} public void insertAtFront(int insertItem){

    if ( isEmpty())firstNode=lastNode=new ListNode(insertItem);

    else firstNode=new ListNode(insertItem, firstNode);

    }

  • 7Ci t Linked ListCi t Linked List

    public void insertAtBack( int insertItem ){if ( isEmpty() )

    firstNode = lastNode = new ListNode( insertItem );else

    lastNode = lastNode.nextNode = new ListNode( insertItem );}public int removeFromFront(){

    int removeItem = -1;if ( ! isEmpty() ) {

    removeItem = firstNode.data; if ( firstNode == lastNode )

    firstNode = lastNode = null;else

    firstNode = firstNode.nextNode;}return removeItem;

    }

  • 8Ci t Linked ListCi t Linked List

    public int removeFromBack(){int removeItem = -1;if ( ! isEmpty(){

    removeItem = lastNode.data; if ( firstNode == lastNode )

    firstNode = lastNode = null;else{

    ListNode current = firstNode;while ( current.nextNode != lastNode )

    current = current.nextNode;lastNode = current;current.nextNode = null;

    }}return removeItem;

    }

  • 9Ci t Linked ListCi t Linked List

    public boolean isEmpty(){

    return (firstNode == null);}

    public void print(){

    ListNode node = firstNode;while (node != null){

    System.out.print(node.data + " ");node = node.nextNode;

    }System.out.println("\n");

    }}

  • 10

    StackStack

    Stack l mt cu trc theo kiu LIFO (Last In First Out),phn t vo sau cng s c ly ra trc.

    Hai thao tc c bn trn Stack Chn phn t: Lun chn vo nh Stack (push) Ly phn t: Lun ly ra t nh Stack (pop)

    V d:Stack s = new Stack();s.push(10);s.push(25);while (!s.isEmpty())

    System.out.print(s.pop() + " ");

    Stack s = new Stack();s.push(10);s.push(25);while (!s.isEmpty())

    System.out.print(s.pop() + " ");

  • 11

    Gii thiuGii thiu

    Collection l i tng c kh nng cha cc i tngkhc.

    Cc thao tc thng thng trn collection: Thm/Xo i tng vo/khi collection Kim tra mt i tng c trong collection khng Ly mt i tng t collection Duyt cc i tng trong collection Xo ton b collection

  • 12

    Collections FrameworkCollections Framework

    Cc collection u tin ca Java: Mng Vector: Mng ng Hastable: Bng bm

    Collections Framework (t Java 1.2) L mt kin trc hp nht biu din v thao tc

    trn cc collection. Gip cho vic x l cc collection c lp vi biu

    din chi tit bn trong ca chng.

  • 13

    Collections FrameworkCollections Framework

    Mt s li ch ca Collections Framework Gim thi gian lp trnh Tng cng hiu nng chng trnh D m rng cc collection mi Khuyn khch vic s dng li m chng trnh

  • 14

    Collections FrameworkCollections Framework

    Collections Framework bao gm Interfaces: L cc giao tip th hin tnh cht ca cc

    kiu collection khc nhau nh List, Set, Map. Implementations: L cc lp collection c sn c

    ci t cc collection interfaces. Algorithms: L cc phng thc tnh x l trn

    collection, v d: sp xp danh sch, tm phn t lnnht...

  • 15

    InterfacesInterfaces

    Collection

    Set

    List

    Map

    SortedMap

    SortedSet

  • 16

    Interface CollectionInterface Collection

    Cung cp cc thao tc chnh trn collection nhthm/xo/tm phn t... V d: boolean add(Object element); boolean remove(Object element); boolean contains(Object element); int size(); boolean isEmpty();

    Nu lp ci t Collection khng mun h tr cc thaotc lm thay i collection nh add, remove, clear... nc th tung ra ngoi l UnsupportedOperationException.

  • 17

    Interface ListInterface List

    List k tha t Collection, cung cp thm cc phngthc x l collection kiu danh sch (Danh sch lmt collection vi cc phn t c xp theo ch s).

    Mt s phng thc ca List Object get(int index); Object set(int index, Object o); void add(int index, Object o); Object remove(int index); int indexOf(Object o); int lastIndexOf(Object o);

  • 18

    Interface SetInterface Set

    Set k tha t Collection, h tr cc thao tc x l trncollection kiu tp hp (Mt tp hp yu cu cc phnt phi khng c trng lp).

    Set khng c thm phng thc ring ngoi ccphng thc k tha t Collection.

  • 19

    Interface SortedSetInterface SortedSet

    SortedSet k tha t Set, h tr thao tc trn tp hpcc phn t c th so snh c. Cc i tng avo trong mt SortedSet phi ci t giao tipComparable hoc lp ci t SortedSet phi nhn mtComparator trn kiu ca i tng .

    Mt s phng thc ca SortedSet: Object first(); //ly phn t u tin (nh nht) Object last(); //ly phn t cui cng (ln nht) SortedSet subSet(Object e1, Object e2); //ly mt tp

    cc phn t nm trong khong t e1 ti e2.

  • 20

    Interface MapInterface Map

    Map cung cp cc thao tc x l trn cc bng nh x(Bng nh x lu cc phn t theo kho v khng cc 2 kho trng nhau).

    Mt s phng thc ca Map Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); ...

  • 21

    Interface SortedMapInterface SortedMap

    SortedMap k tha t Map, n cung cp thao tc trncc bng nh x vi kho c th so snh c.

    SortedSet, cc i tng kha a vo trongSortedMap phi ci t giao tip Comparable hoc lpci t SortedMap phi nhn mt Comparator trn itng kho.

  • 22

    ImplementationsImplementations

    Cc ci t trong Collections Framework chnh l cclp collection c sn trong Java. Chng ci t cccollection interface trn th hin cc cu trc dliu c th. V d: mng ng, danh sch lin kt, cy en, bng bm...

  • 23

    ImplementationsImplementations

    ListLinkedList

    ArrayList

    Map LinkedHashMap

    SortedMap

    HashMap

    TreeMap

    Set

    HashSet

    LinkedHashSet

    SortedSet TreeSet

  • 24

    M t cc ci tM t cc ci t

    ArrayList: Mng ng, nu cc phn t thm vo vtqu kch c mng, mng s t ng tng kch c.

    LinkedList: Danh sch lin kt 2 chiu. H tr thao tctrn u v cui danh sch.

    HashSet: Bng bm. LinkedHashSet: Bng bm kt hp vi linked list nhmm bo th t cc phn t.

    TreeSet: Cy en (red-black tree).

  • 25

    V d 1V d 1

    import java.util.*;public class LinkedListDemo {

    public static void main(String args[]){

    LinkedList l = new LinkedList();l.add(7);l.add(10);l.addFirst(1);l.addLast(20);for (int i = 0; i

  • 26

    V d 2V d 2

    Set s1 = new HashSet();s1.add(3);s1.add(5);s1.add(7);s1.add(9);

    Set s2 = new HashSet();s2.add(3);s2.add(4);s2.add(6);s2.add(8);

    System.out.println(s1);System.out.println(s2);

    s1.addAll(s2);System.out.println(s1);

  • 27