62,621
社区成员
发帖
与我相关
我的任务
分享import java.util.*;
/**用泛型来实现一个MyMap
*有一个put方法,三个get方法,一个contains方法,
*可以根据自己的需求再添加其它的方法,比如把所有记录都提出来的方法getAll()。
*/
public class MyMap<T1,T2,T3,T4>{
/**内部类,用来表示一条记录。
*/
class Record{
T1 f1;
T2 f2;
T3 f3;
T4 f4;
Record(T1 f1,T2 f2,T3 f3,T4 f4){
this.f1=f1;
this.f2=f2;
this.f3=f3;
this.f4=f4;
}
public String toString(){
return "["+f1.toString()+","+f2.toString()+","+f3.toString()+","+f4.toString()+"]";
}
}
private HashMap<T1,HashMap<T2,HashMap<T3,HashSet<T4>>>> hashMap=new HashMap<T1,HashMap<T2,HashMap<T3,HashSet<T4>>>>();
/**把一条记录放入MyMap
*可以再写一个只有一个参数的方法void put(MyMap<T1,T2,T3,T4>.Record record)。
*调用一下put(record.f1,record.f2,record.f3,record.f4)就可以了。
*/
public void put(T1 field1,T2 field2,T3 field3,T4 field4){
HashMap<T2,HashMap<T3,HashSet<T4>>> m2=hashMap.get(field1);
if(m2!=null){
HashMap<T3,HashSet<T4>> m3=m2.get(field2);
if(m3!=null){
HashSet<T4> s4=m3.get(field3);
if(s4!=null){
s4.add(field4);
}else{
s4=new HashSet<T4>();
s4.add(field4);
m3.put(field3,s4);
}
}else{
m3=new HashMap<T3,HashSet<T4>>();
HashSet<T4> s4=new HashSet<T4>();
s4.add(field4);
m3.put(field3,s4);
m2.put(field2,m3);
}
}else{
m2=new HashMap<T2,HashMap<T3,HashSet<T4>>>();
HashMap<T3,HashSet<T4>> m3=new HashMap<T3,HashSet<T4>>();
HashSet<T4> s4=new HashSet<T4>();
s4.add(field4);
m3.put(field3,s4);
m2.put(field2,m3);
hashMap.put(field1,m2);
}
}
/**返加第一个字段相同的记录,结果是一个ArrayList
*
*/
public ArrayList<Record> getRecord(T1 field1){
HashMap<T2,HashMap<T3,HashSet<T4>>> m2=hashMap.get(field1);
if(m2!=null){
ArrayList<Record> al=new ArrayList<Record>();
for(T2 f2:m2.keySet()){
HashMap<T3,HashSet<T4>> m3=m2.get(f2);
for(T3 f3:m3.keySet()){
HashSet<T4> s4=m3.get(f3);
for(T4 f4:s4){
al.add(new Record(field1,f2,f3,f4));
}
}
}
return al;
}
return null;
}
/**返回第一个字段,第二个字段相同的记录,结果是一个ArrayList.
*
*/
public ArrayList<Record> getRecord(T1 field1,T2 field2){
HashMap<T2,HashMap<T3,HashSet<T4>>> m2=hashMap.get(field1);
if(m2!=null){
HashMap<T3,HashSet<T4>> m3=m2.get(field2);
if(m3!=null){
ArrayList<Record> al=new ArrayList<Record>();
for(T3 f3:m3.keySet()){
HashSet<T4> s4=m3.get(f3);
for(T4 f4:s4){
al.add(new Record(field1,field2,f3,f4));
}
}
return al;
}
}
return null;
}
/**返回第一个字段,第二个字段,第三个字段相同的记录,结果是一个ArrayList.
*
*/
public ArrayList<Record> getRecord(T1 field1,T2 field2,T3 field3){
HashMap<T2,HashMap<T3,HashSet<T4>>> m2=hashMap.get(field1);
if(m2!=null){
HashMap<T3,HashSet<T4>> m3=m2.get(field2);
if(m3!=null){
HashSet<T4> s4=m3.get(field3);
if(s4!=null){
ArrayList<Record> al=new ArrayList<Record>();
for(T4 f4:s4){
al.add(new Record(field1,field2,field3,f4));
}
return al;
}
}
}
return null;
}
/**判断是否包含某条记录。
*/
public boolean contains(T1 field1,T2 field2,T3 field3,T4 field4){
HashMap<T2,HashMap<T3,HashSet<T4>>> m2=hashMap.get(field1);
if(m2!=null){
HashMap<T3,HashSet<T4>> m3=m2.get(field2);
if(m3!=null){
HashSet<T4> s4=m3.get(field3);
if(s4!=null){
return s4.contains(field4);
}
}
}
return false;
}
/*测试MyMap类。
*/
public static void main(String[] args){
String[][] records={
{"1","a1","b1","c1"},
{"1","a1","b1","c2"},
{"1","a1","b1","c3"},
{"1","a1","b2","c21"},
{"1","a1","b2","c1"},
{"1","a1","b2","c22"},
{"1","a2","b21","c211"},
{"1","a2","b22","c1"},
{"1","a2","b1","c1"},
{"2","a1","b1","c1"},
{"2","a1","b1","c1"},
{"2","a1","b1","c1"},
{"2","a1","b1","c2"},
{"2","a1","b1","c3"},
{"2","a1","b2","c21"},
{"2","a1","b2","c1"},
{"2","a1","b2","c22"},
{"2","a2","b21","c211"},
{"2","a2","b22","c1"},
{"2","a2","b1","c1"},
};
//创建MyMap类的对象用以下格式:
//
MyMap<String,String,String,String> myMap=new MyMap<String,String,String,String>();
//给myMap添加数据:
//
for(String[] strs:records){
myMap.put(strs[0],strs[1],strs[2],strs[3]);
}
System.out.println("myMap的大小是:"+myMap.hashMap.size());
//从myMap中提取A为"1"的数据用下面的格式,返回的是一个ArrayList:
//
ArrayList<MyMap<String,String,String,String>.Record> list1=myMap.getRecord("1");
System.out.println("----------myMap.get(\"1\")-----------");
for(MyMap.Record re:list1){
System.out.println(re);
}
System.out.println("-------End myMap.get(\"1\")--------");
//从myMap中提取A为"1",B为"a1"的数据用下面的格式,返回的是一个ArrayList:
//
ArrayList<MyMap<String,String,String,String>.Record> list2=myMap.getRecord("1","a1");
System.out.println("\n\n----------myMap.get(\"1\",\"a1\")-----------");
for(MyMap.Record re:list2){
System.out.println(re);
}
System.out.println("-------End myMap.get(\"1\",\"a1\")--------");
//从myMap中提取A为"1",B为"a1",C为"b1"的数据用下面的格式,返回的是一个ArrayList:
//
ArrayList<MyMap<String,String,String,String>.Record> list3=myMap.getRecord("1","a1","b1");
System.out.println("\n\n----------myMap.get(\"1\",\"a1\",\"b1\")-----------");
for(MyMap.Record re:list3){
System.out.println(re);
}
System.out.println("-------End myMap.get(\"1\",\"a1\",\"b1\")--------");
System.out.println("是否包含1,a1,b2,c2 ?:"+myMap.contains("1","a1","b2","c2"));
System.out.println("是否包含2,a2,b22,c1 ?:"+myMap.contains("2","a2","b22","c1"));
}
}