大家好我相对数据库查询出的数据存放到Map里请求解决办法,问题是这样的
数据库表如:
  A   B    C     D   
  1   a1   cc    dd
  1   a2   ccc   ddd
  1   a3   cccc  dddd
  2   b1   v     j 
  2   b2   vv    jj 
  2   b3   vvv   jjj 希望读取出来存到Map里的格式是
map.put(1,map2.put(a1,map3(C,cc)));map.put(1,map2.put(a2,map3(C,ccc)));map.put(1,map2.put(a3,map3(C,cccc)));
map.put(1,map2.put(a1,map3(D,dd)));map.put(1,map2.put(a2,map3(D,ddd)));map.put(1,map2.put(a3,map3(D,dddd)));map.put(2,map2.put(b1,map3(C,v)));map.put(2,map2.put(b2,map3(C,vv)));map.put(2,map2.put(b3,map3(C,vvv)));
map.put(2,map2.put(b1,map3(D,j)));map.put(2,map2.put(b2,map3(D,jj)));map.put(2,map2.put(b3,map3(D,jjj)));急求就绝办法......多谢各位大侠了,在线等解决办法

解决方案 »

  1.   

    先写一个数据库表的实体对象类
    例如: class Data{
        private  int a;
        private  String b;
        private  String c;
        private  String d;
         set()
         get()
         .....
    }
    得到是每一行数据就组装一个实体对象:
     Data d1 = new Data();
    .....然后放到map中,map.put(1,d1);map.put(1,d2);......用的时候你也知道了.得到对象,也就得到数据了.
    说的已经很通俗了吧
      

  2.   

    我估计你是想存成一个树的格式
    但是map.put(key,value)中,key值是唯一的,
    所以你最终只有两个键1和2
      

  3.   

    大概模型根树一样!!希望高手提供详细代码!!
    不要像二楼的建一个实体对象之类的,没必要!
      我就是ResultSet 把数据读出来,然后根据条件存数据
      

  4.   

    你是想做到x-y轴那样来确定元素吧,你可以先对key进行封装,如:
    public class Key {
        
       private String x;   private String y;   public Key(String x, String y) {
         this.x = x;
         this.y = y;
       }   //setter和getter略……
    }然后你可以这样子的
    map.put(new Key(x,y),value);
    具体问题具体对待,可以根据你的要求对Key类进行修改
      

  5.   

    ....
      真不清楚是否可以实现 
     map.put(key,value) 放入同一个key ..
      

  6.   


    不要想得太呆板了!! 可以的! 你可以想成树的结构就可以了!  map里面是不可以防同一个key,我这里这么写是想说明我是怎么找数据的!存的话跟树一样
      

  7.   

    你至少要把A,B,C,D四个字段的类型说明一下吧。都是字符串吗?
    你做成这种结构,在这种结构上要做那些操作,比如需要把记录还原回去吗?给定A,B是不是要把所有C,D求出来?等等。
      

  8.   


    类型不用管都是字符串
    给定A B 可以精确的找到 C  D 对应的值
      

  9.   


    急求就绝办法......多谢各位大侠了,在线等解决办法
    用二维数组啊!!!将上面的“A空格B空格C空格D换行1空格a1空格cc空格dd换行。。”用String的split方法来分别以空格和换行符来存入2维数组,用2次split方法来实现,第一次是split换行,第二次是split空格,
    然后用for循环存入map,就可以了
      

  10.   

    给定A B 以什么形式返回 C D
      

  11.   

    Map<String,Map<String,Map<String,String>>> map = new HashMap<String,Map<String,Map<String,String>>>();//总的map就一个
    //每次读取一行数据,就的重新建立map2、map3,这样会有很多Map,不知道你的需求允许么
    Map<String,String> map3 = new HashMap<String,String>();
    map3.put("C", "c");
    map3.put("D", "d");
    Map<String,Map<String,String>> map2 = new HashMap<String,Map<String,String>>();
    map2.put("a1", map3);
    map.put("1", map2);
      

  12.   

    写了一个,挺烦的.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"));
    }
    }
      

  13.   

    测试结果:
    F:\java>java MyMap
    myMap的大小是:2
    ----------myMap.get("1")-----------
    [1,a1,b1,c1]
    [1,a1,b1,c2]
    [1,a1,b1,c3]
    [1,a1,b2,c1]
    [1,a1,b2,c22]
    [1,a1,b2,c21]
    [1,a2,b1,c1]
    [1,a2,b21,c211]
    [1,a2,b22,c1]
    -------End   myMap.get("1")--------
    ----------myMap.get("1","a1")-----------
    [1,a1,b1,c1]
    [1,a1,b1,c2]
    [1,a1,b1,c3]
    [1,a1,b2,c1]
    [1,a1,b2,c22]
    [1,a1,b2,c21]
    -------End   myMap.get("1","a1")--------
    ----------myMap.get("1","a1","b1")-----------
    [1,a1,b1,c1]
    [1,a1,b1,c2]
    [1,a1,b1,c3]
    -------End   myMap.get("1","a1","b1")--------
    是否包含1,a1,b2,c2 ?:false
    是否包含2,a2,b22,c1 ?:true
      

  14.   

    id:指的是 A
    serId:指的是B
    public Map map_date(String sql,String id,String serId) throws SQLException{
    Map map=new HashMap();
    Map map_date=new HashMap();
    rowName = this.oldQueryRow(table);  //这是除了两个A,B 的所有列名
    ResultSet rs=null;
    try {
    String idKey=null;
    rs=con.createStatement().executeQuery(sql);
    while(rs.next()){
    idKey=rs.getString(id);
    String seridKey=rs.getString(serId);
    Map map2=new HashMap();
    for(Object columnName : rowName){
    map2.put(columnName.toString(), rs.getString(columnName.toString()));
    }
    Map map1=(Map) map_date.get(idKey);
    if(map1==null){
    map1=new HashMap();
    }
    map1.put(seridKey, map2);
    map_date.put(idKey, map1);
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }finally{
    con.close();
    rs.close();
    }
    return map;
    }
    最后格式是  <A,Map <B,Map <列名,值>>>