By GokiSoft.com|
15:07 26/04/2020|
Java Advanced
Share Code - Tìm hiểu Collections & Generic trong Java
Share Code - Tìm hiểu Collections & Generic trong Java
#Source Code Dự Án
##Class Main
/*
* 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 lession2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
//Collection
//Phan 1 => List
//Class Object >> List >> ArrayList
List<Integer> list = new ArrayList<>();//Tinh chat da hinh trong lap trinh
//Them phan tu vao trong mang
list.add(1);
list.add(10);
System.out.println("=============== Turn #1");
for (Integer v : list) {
System.out.println(">> " + v);
}
list.add(1, 12);
System.out.println("=============== Turn #2");
for (Integer v : list) {
System.out.println(">> " + v);
}
//Xoa 1 phan tu khoi mang
list.remove(1);
System.out.println("=============== Turn #3");
for (Integer v : list) {
System.out.println(">> " + v);
}
List<String> list2 = new ArrayList<>();
list2.add("Phan tu 1");
list2.add("Phan tu 2");
list2.add("Phan tu 3");
System.out.println("=============== String Turn #1");
for (String v : list2) {
System.out.println(">> " + v);
}
list2.remove("Phan tu 2");
System.out.println("=============== String Turn #2");
for (String v : list2) {
System.out.println(">> " + v);
}
list2.clear();
//duyet phan tu trong mang
list.get(0);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
for (String string : list2) {
System.out.println(string);
}
list2.forEach((string) -> {
System.out.println(string);
});
//Phan 2 >> Vector
List<Integer> list3 = new Vector<>();//Tinh chat da hinh trong lap trinh
list3.add(123213);
//Map
Map<String, Integer> map = new HashMap<>();
map.put("k1", 1000);
map.put("k1", 23);
map.put("k2", 43);
//cach 1 => truy xuat du lieu theo key
int v = map.get("k2");
System.out.println(v);
//Student => rollNo => duy nhat => primary key
//StudentA => R001 => Map => R001 => StudentA
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("key = " + key);
System.out.println("value = " + String.valueOf(value));
}
// DataMgr<Integer> dataMgr = DataMgr.getInstance();
// dataMgr.add(12);
DataMgr<People> dataMgr2 = DataMgr.getInstance();
dataMgr2.add(new People());
// DataMgr<String> dataMgr3 = DataMgr.getInstance();
// dataMgr3.add("String");
//
// Object obj = dataMgr.get(0);
// if(obj instanceof People) {
// System.out.println("People");
// } else {
// System.out.println("Other");
// }
}
}
##Class People
/*
* 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 lession2;
/**
*
* @author Diep.Tran
*/
public class People {
}
##Class Student => Kế Thừa People
/*
* 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 lession2;
/**
*
* @author Diep.Tran
*/
public class Student extends People{
}
##Class DataMgr => Quản lý dữ liệu
/*
* 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 lession2;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Diep.Tran
*/
public class DataMgr<E extends People> {
List<E> list = new ArrayList<>();
private static DataMgr instance = null;
private DataMgr() {
}
public synchronized static DataMgr getInstance() {
if(instance == null) {
instance = new DataMgr();
}
return instance;
}
public void add(E e) {
list.add(e);
}
public E get(int index) {
return list.get(index);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)