Untitled
fttk
plain_text
2 years ago
3.6 kB
3
Indexable
import java.util.ArrayList; import java.util.HashSet; import java.util.HashMap; public class UserSolution { class Record{ String[] value; boolean isDel;//Them vao de danh dau 1 ban ghi da bi xoa hay chua de khong phai xoa that trong hashmap public Record(String name, String number, String birthday, String email, String memo) { this.value = new String[5]; this.value[0] = name; this.value[1] = number; this.value[2] = birthday; this.value[3] = email; this.value[4] = memo; this.isDel = false; } } HashMap<String, ArrayList<Record>>[] hmRecords;//5 hashmap luu cac ban ghi theo 5 truong gia tri public void InitDB() { hmRecords = new HashMap[5]; for(int i=0; i<5; i++) { hmRecords[i] = new HashMap<>(); } } public void Add(String name, String number, String birthday, String email, String memo) { Record newRecord = new Record(name, number, birthday, email, memo);//Tao 1 doi tuong ban ghi moi for(int i=0; i<5; i++) { ArrayList<Record> list = hmRecords[i].get(newRecord.value[i]);//Lay ra list trong hashmap if(list == null) {//Neu chua co list nao trong hashmap list = new ArrayList<>(); hmRecords[i].put(newRecord.value[i], list); } list.add(newRecord);//Neu da co roi thi chi can them vao list hien tai } } public int Delete(int field, String str) { int count = 0; ArrayList<Record> list = hmRecords[field].get(str);//Lay ra list tu truong thong tin trong hashmap tuong ung if(list != null) { for(Record record : list) { if(record.isDel == false) { record.isDel = true;//Chi can danh dau ban ghi da bi xoa ma khong can xoa trong hashmap count++; } } } return count; } public int Change(int field, String str, int changefield, String changestr) { Record newRecord = null; int count = 0; ArrayList<Record> listRecord = hmRecords[field].get(str); if(listRecord != null) { int size = listRecord.size(); for(int i=0; i<size; i++) { Record rc = listRecord.get(i); if(rc.isDel == false) { newRecord = rc; rc.isDel = true; newRecord.value[changefield] = changestr; count++; Add(newRecord.value[0], newRecord.value[1], newRecord.value[2], newRecord.value[3], newRecord.value[4]);//Them ban ghi voi gia tri moi vao hashmap } } } return count; } public Solution.Result Search(int field, String str, int returnfield) { Solution.Result s = new Solution.Result(); s.count = 0; s.str = ""; ArrayList<Record> listRecord = hmRecords[field].get(str); if(listRecord != null) { for(Record record : listRecord) { if(record.isDel == false) { s.count++; s.str = record.value[returnfield]; } } } if(s.count > 1) s.str = ""; return s; } }
Editor is loading...