Indranil Banerjee
Indranil Banerjee

Reputation: 133

How to put arraylist into Hashmap in dynamic way?

public HashMap viewTableData(String tabName)
   {
       ArrayList<String> arrayList = new ArrayList<String>(); 
       ArrayList<String> arrayList2 = new ArrayList<String>(); 
       ArrayList<String> arrayList3 = new ArrayList<String>(); 
       ArrayList<String> arrayList4 = new ArrayList<String>(); 
       HashMap discoverMap = new HashMap();

        try {
            int i=1;
            con = DAOConnection.sqlconnection();
            stmt = con.createStatement();
            stmt = con.createStatement();
            query = "SELECT * FROM "+tabName;
            System.out.println("Qry executed");
            ps = con.prepareStatement(query);
            rs = ps.executeQuery(query);
           while(rs.next()){
              while(i<=4){ 
               arrayList.add(rs.getString(i));
               //arrayList2.add(rs.getString(2));
              discoverMap.put("qrycol"+i, arrayList);
              i++;
           }
           }
          // discoverMap.put("qrycol1", arrayList);
          // discoverMap.put("qrycol2",arrayList2);
           System.out.println("Map result:"+discoverMap);
        } catch (SQLException ex) {
            Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
            //return null;
        }
        return discoverMap;
   }

Here you can see that, I have written a method which will take the values from database table and put into Hashmap.

If I initialize 4 different arraylist, and add rs.getString(1),rs.getString(2),... then it'sworking fine. But I want some dynamic feature which will create desire arraylist and add the rs.getString(i). But it's running but not getting desire output. Here discoverMap showing result: Map result:{qrycol1=[1, Indranil, Banerjee, [email protected]], qrycol3=[1, Indranil, Banerjee,[email protected]], qrycol2=[1, Indranil, Banerjee, [email protected]], qrycol4=[1, Indranil, Banerjee, [email protected]]}

But for code below showing desire output :

public HashMap viewTableData(String tabName)
   {
       ArrayList<String> arrayList = new ArrayList<String>(); 
       ArrayList<String> arrayList2 = new ArrayList<String>(); 
       ArrayList<String> arrayList3 = new ArrayList<String>(); 
       ArrayList<String> arrayList4 = new ArrayList<String>(); 
       HashMap discoverMap = new HashMap();

        try {
            int i=1;
            con = DAOConnection.sqlconnection();
            stmt = con.createStatement();
            stmt = con.createStatement();
            query = "SELECT * FROM "+tabName;
            System.out.println("Qry executed");
            ps = con.prepareStatement(query);
            rs = ps.executeQuery(query);
           while(rs.next()){
              arrayList.add(rs.getString(1));
              arrayList2.add(rs.getString(2));
              arrayList3.add(rs.getString(3));
              arrayList4.add(rs.getString(4));
           }
            discoverMap.put("qrycol1", arrayList);
            discoverMap.put("qrycol2",arrayList2);
            discoverMap.put("qrycol3",arrayList3);
            discoverMap.put("qrycol4",arrayList4);
           System.out.println("Map result:"+discoverMap);
        } 

        catch (SQLException ex) {
            Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
            //return null;
        }
        return discoverMap;
   }

Map result:{qrycol1=[1, 2, 3, 4, 6, 5, 7, 8], qrycol3=[Banerjee, JJJ, Saun, Gupta, efgh,aaaa, surname22, Sir], qrycol2=[Indranil, pora, trao, Som, James, Anup, Abhi, Mia], qrycol4=[[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]]}

This is actually correct.But I want it dynamically. Since all the time we can not assume how many arraylist require. It would be very helpful if someone kindly help on this. Thanks in advance .

Database table structure : There are 4 columns and type varchar(but it may different for other table, that's why I asked this question. )

Upvotes: 1

Views: 518

Answers (1)

Bon
Bon

Reputation: 3103

You need to know the number of columns returned from the database, if you know that you can follow:

        int numOfColumns = 6; // it can be any number you need

        // initialize the map
        for (int colIndex = 1; colIndex <= numOfColumns; ++colIndex) {
            String key = "qrycol" + colIndex;
            ArrayList<String> list = new ArrayList<>();
            discoverMap.put(key, list);
        }

        while (rs.next()) {
            // for each column got from DB, populate the corresponding array list
            for (int colIndex = 1; colIndex <= numOfColumns; ++colIndex) {
                String key = "qrycol" + colIndex;
                ArrayList<String> list = (ArrayList<String>) discoverMap.get(key);
                list.add(rs.getString(colIndex));
            }
        }

Upvotes: 1

Related Questions