By GokiSoft.com| 10:11 11/08/2021|
Java Advanced

[Share Code] Tìm hiểu Collections + + Map - Generic - Lập trình Java


Nội dung kiến thức:
- Collections
	- List => ArrayList, Vector, LinkedList
	- Map => HashMap, ...
	- Set => ...
	- Stack/Queue => ???
		- Stack -> Quan ly mang theo kieu ngan
			Xep sach. (LIFO) last in first out
			A1 -> A2 -> A3 -> A4 -> A5
			push (pull) -> dat sach vao dau
			pop -> lay sach
		- Queue -> Quan ly theo kieu hang doi (FIFO) first in first out
			Xep mua ve xem phim
			A5 -> A4 -> A3 -> A2 -> A1 -> pick
			push (pull) -> xep vao cuoi
			pop -> lay tu da ra
- Generic trong lập trình Java




#Test.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lesson02;

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        //Tim hieu Generic
        ArrayList<Integer> a1 = new ArrayList<>();
        a1.add(1);
        a1.add(5);
        
        int t = a1.get(0);
        
        ArrayList<String> a2 = new ArrayList<>();
        a2.add("S1");
        a2.add("S2");
        
        String k = a2.get(0);
        System.out.println(k);
        
        //Cau 1: ArrayList<Integer> & ArrayList<String> -> No la may lop doi tuong
        //Reply: Chi 1 lop doi tuong.
        
        //Cau 2: ArrayList<Integer> -> Tai sao ko them dc String
        //Integer & String => OK
        //Hieu xau hon ???
        
        DataController<Book> dataController = new DataController<>();
        dataController.add(new Book("1", "Lap Trinh A"));
        dataController.add(new Book("2", "Lap Trinh B"));
        dataController.add(new Book("3", "Lap Trinh C"));
        
        dataController.showData();
        
//        DataController<String> d2 = new DataController<>();
//        d2.add("A1");
//        d2.add("A2");
//        d2.add("A3");
//        
//        d2.showData();
    }
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lesson02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Phan 1: List => Interface.
        //ArrayList => Lop doi tuong => Cho phep quan ly du lieu dong. => implement List
        ArrayList<String> arr1 = new ArrayList<>();
        //Khoi tao => elementData -> mang rong -> thuc hien them 1 phan tu moi vao
        //elementData -> so phan tu la 10 -> add phan tu vao mang elementData
        //So luong phan tu them vao du 10 -> bat dau tu phan tu so 11 -> elementData -> moi lan tang len 1 khong gian bo nho
        //Them phan tu vao mang
        arr1.add("S1");//index = 0
        System.out.println("Length: " + arr1.size());
        arr1.add("S2");//index = 2
        arr1.add(1, "S123");//index = 1
        arr1.add("S3");//index = 3
        //Length: arr1.size()
        //Sua du lieu
        arr1.set(1, "S22");
        for (String v : arr1) {
            System.out.println(v);
        }
        
        //Xoa 1 phan tu khoi mang
        arr1.remove(1);
        System.out.println("Du lieu sau khi xoa ...");
        for (String v : arr1) {
            System.out.println(v);
        }
        List<String> a1 = new ArrayList<>();
        a1 = new Vector<>();
        
        //Tim hieu List => ArrayList, Vector, LinkedList
        Vector<String> arr2 = new Vector<>();
        //Tim hieu ve Vector
        //Khoi tao -> elementData mang gom 10 phan tu
        //elementData -> 10 -> khi add them phan tu thu 11 -> tang 10 vung nho thoi
        arr2.add("S22");
        arr2.add("S32");
        arr2.add("S12");
        arr2.add("S52");
        System.out.println("Du lieu arr2");
        for (String v : arr2) {
            System.out.println(v);
        }
        //ArrayList, Vector => Khi thuc hien tang o nho 10 -> 20, ... => Tao ra vung nho moi gom 20, ... => Thuc hien copy o nho cu sang o nho moi
        
        //Tim hieu LinkedList
        LinkedList<String> arr3 = new LinkedList<>();
        arr3.add("S1");
        arr3.add("S2");
        arr3.add("S3");
        arr3.add("S4");
        arr3.add("S5");
        
        //Phan 2: Set, HashSet, TreeSet
        Set<String> set = new HashSet<>();
        set.add("A1");
        set.add("A2");
        set.add("A3");
        
        for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
            String next = iterator.next();
            System.out.println(next);
        }
        
        for (String v : set) {
            System.out.println(v);
        }
        
        //Phan 3: Map
        //Quan ly du lieu key => value (Giong vs localStorage (js), $_COOKIE (php), $_SESSION (php), ...
        Map<String, Book> map = new HashMap<>();
        //Them phan tu vao trong Map
        map.put("S1", new Book("B01", "LAP TRINH C"));
        map.put("S1", new Book("B03", "LAP TRINH PHP"));
        map.put("S1", new Book("B04", "LAP TRINH MySQL")); //Phan tu nay de len cac phan tu truoc.(Phan tu truoc bi xoa di)
        map.put("S2", new Book("B02", "LAP TRINH JAVA"));
        
        //Lay phan tu trong Map
        Book b = map.get("S1");
        if(b != null) {
            System.out.println(b);//In ra => LAP TRINH MySQL
        }
        
        b = map.get("S3");
        if(b != null) {
            System.out.println(b);
        } else {
            System.out.println("Ko tim thay");
        }
    }
}


#DataController.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lesson02;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diep.Tran
 * @param <E>
 */
public class DataController<E extends Book> {
    List<E> elemantData;
    
    public DataController() {
        elemantData = new ArrayList<>();
    }
    
    public void add(E e) {
        elemantData.add(e);
    }
    
    public void showData() {
        for (E e : elemantData) {
            System.out.println(e);
        }
    }
}


#Book.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package java2.lesson02;

/**
 *
 * @author Diep.Tran
 */
public class Book {
    String bookCode;
    String bookName;

    public Book(String bookCode, String bookName) {
        this.bookCode = bookCode;
        this.bookName = bookName;
    }

    public Book() {
    }

    public String getBookCode() {
        return bookCode;
    }

    public void setBookCode(String bookCode) {
        this.bookCode = bookCode;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    @Override
    public String toString() {
        return "Book{" + "bookCode=" + bookCode + ", bookName=" + bookName + '}';
    }
    
    
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)