在HashMap中重新排列唯一的HashMap值<string []>或HashMap <arraylist> </arraylist> </string []>

时间:2014-11-07 20:05:43

标签: hashmap

在我的表格中,我的数据如下,

用户名年龄
安瓦尔26
Ragul 25
帕布26
Praveen 25
湿婆26
伊姆兰26
Rajkumar 25
Vinoth 24

在这里,我想根据用户的年龄对用户名进行分类,并将其存储在字符串数组中,目前我编码如下,并且工作正常。但我希望简化它。如果有人有任何想法,请分享。谢谢

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Options {

    static String URL = "jdbc:derby://localhost:1527/sample;create=true";
    static String USER = "admin";
    static String PASS = "admin";

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Connection conn = null;
        ResultSet rs=null;
        Statement stmt = null;

        HashMap<String,Integer> allValue = new HashMap<String,Integer> (); 
        HashMap<Integer,String> uniqValue = new HashMap<Integer,String> ();
        HashMap<Integer,String[]> newMap = new HashMap<Integer,String[]> ();

        String name[] =null;

        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            conn = DriverManager.getConnection(URL,USER,PASS);
            stmt = conn.createStatement();

            rs=stmt.executeQuery("select * from userDetails");  

            while(rs.next()){  
                allValue.put(rs.getString(1), rs.getInt(2));
                uniqValue.put(rs.getInt(2), rs.getString(1));
            } 

            Iterator uniqEntries = uniqValue.entrySet().iterator();

            while (uniqEntries.hasNext()) {
                int j=0;
                Map.Entry entry = (Map.Entry) uniqEntries.next();
                Integer key = (Integer)entry.getKey();
                String value = (String) entry.getValue();

                Iterator allEntries = allValue.entrySet().iterator();
                name = new String[5];
                    while (allEntries.hasNext()) {
                        Map.Entry allentry = (Map.Entry) allEntries.next();
                        String allkey = (String)allentry.getKey();
                        Integer allvalue = (Integer) allentry.getValue();

                            if(key == allvalue){
                                name[j] = allkey;   
                                j++;
                            }
                    }
                    newMap.put(key, name);
            }

             Iterator newEntries = newMap.entrySet().iterator();
                while (newEntries.hasNext()) {
                    Map.Entry newentry = (Map.Entry) newEntries.next();
                    Integer newkey = (Integer)newentry.getKey();
                    String[] newValue = (String[])newentry.getValue();

                    System.out.println("Age "+newkey);
                    for(int n=0;n<5;n++){
                        if(newValue[n] != null){
                            System.out.println("User "+newValue[n]);    
                        }

                    }
                    System.out.println("\n");                       
                }

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

输出:

年龄25岁 用户Rajkumar
用户Praveen
用户Ragul

年龄24岁 用户Vinoth

年龄26岁 用户Anvar
用户Imran
用户Siva
用户Prabhu

1 个答案:

答案 0 :(得分:0)

一种方法是首先按age命令从数据库中检索结果。

    rs=stmt.executeQuery("select * from userDetails order by age desc"); 

然后,制作一张地图values,其中包含年龄与该年龄段用户列表之间的映射。

    Map<Integer,List<String>> values = new HashMap<Integer,List<String>>();
    List<String> names = new ArrayList<String>();
    int prevAgeGroup = -1;
    int currentAgeGroup = 0;
    while(rs.hasNext())
    {
        currentAgeGroup = rs.getInt(2);
        if(currentAgeGroup != prevAgeGroup && prevAgeGroup > -1)
        {
            values.put(prevAgeGroup, names);
            names = new ArrayList<String>();
        }
        names.add("user "+rs.getString(1));
        prevAgeGroup = currentAgeGroup;
    }
    values.put(currentAgeGroup, names);

上面的代码,迭代数据库中的每个条目,因为所有条目都按年龄排序,我们可以按顺序检查当前年龄是否与上次遇到的年龄不同。如果是,则创建一个新列表,用于存储当前年龄的用户列表。

您还可以查看MultiHashMap,以更简单的方式实现此功能。