Java 向hashmap添加长键和arraylist值

Java 向hashmap添加长键和arraylist值,java,arraylist,hashmap,Java,Arraylist,Hashmap,我用这种方式获取数据库数据 用户ID,日期 1125,3-05-2013 1125,4-05-2013 1125,5-05-2013 200,23-05-2013 200,24-05-2013 我需要将这些作为hashmap(userid,dates)添加到hashmap中 i、 e:hashmap(长,arraylist(日期字符串))并发送到前端 我的意思是hashmap中的long值应该是唯一的,这是检索特定用户id的所有日期列表的键 所以,如果我尝试hashmap.get(1125)=I

我用这种方式获取数据库数据

用户ID,日期 1125,3-05-2013

1125,4-05-2013

1125,5-05-2013

200,23-05-2013

200,24-05-2013

我需要将这些作为hashmap(userid,dates)添加到hashmap中

i、 e:hashmap(长,arraylist(日期字符串))并发送到前端

我的意思是hashmap中的long值应该是唯一的,这是检索特定用户id的所有日期列表的键

所以,如果我尝试
hashmap.get(1125)=I
应该得到用户1125的所有日期列表,比如3-05-2013,4-05-2013,5-05-2013

然后,如果我尝试
hashmap.get(200)=I
应该得到用户200的所有日期列表,比如2013年5月23日、2013年5月24日

我试过这种方法,但是我得到了单个用户ID的所有日期,比如

用户200 日期[2013年5月3日、2013年5月4日、2013年5月5日、2013年5月23日、2013年5月24日]

这是我的密码

// TODO Auto-generated method stub

        List<User> myEmpls = new ArrayList<User>();

        User User1 = new User();
        User1.setEmpcode((long) 1125);
        User1.setDate("3-05-2013");
        myEmpls.add(User1);

        User User2 = new User();
        User2.setEmpcode((long) 1125);
        User2.setDate("4-05-2013");
        myEmpls.add(User2);

        User User5 = new User();
        User5.setEmpcode((long) 1125);
        User5.setDate("5-05-2013");
        myEmpls.add(User5);

        User User3 = new User();
        User3.setEmpcode((long) 200);
        User3.setDate("23-05-2013");
        myEmpls.add(User3);

        User User4 = new User();
        User4.setEmpcode((long) 200);
        User4.setDate("24-05-2013");
        myEmpls.add(User4);



        long prevUser=0;
        int cnt=1;
        long users =0;
        ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
        HashMap<Long, ArrayList> finalmap =  new HashMap<>();
        ArrayList<String> dates = new ArrayList<>();
        for(User time : myEmpls)
        {
            if(prevUser==time.getEmpcode())
            {
                users = time.getEmpcode();
                System.out.println("users"+users);
                dates.add(time.getDate());
            }
            else
            {
                dates.add(time.getDate());
            }
            System.out.println("dates"+dates);
            finalmap.put(users, lists);
            prevUser =  time.getEmpcode();
            cnt++;
        }
//TODO自动生成的方法存根
List myEmpls=new ArrayList();
用户User1=新用户();
User1.setEmpcode((长)1125);
用户1.设定日期(“3-05-2013”);
myEmpls.add(User1);
用户User2=新用户();
User2.setEmpcode((长)1125);
用户2.设定日期(“4-05-2013”);
myEmpls.add(User2);
User User5=新用户();
User5.setEmpcode((长)1125);
用户5.设定日期(“5-05-2013”);
myEmpls.add(User5);
User User3=新用户();
User3.setEmpcode((长)200);
用户3.设定日期(“23-05-2013”);
myEmpls.add(User3);
User User4=新用户();
User4.setEmpcode((长)200);
用户4.设定日期(“24-05-2013”);
myEmpls.add(User4);
长用户=0;
int-cnt=1;
长用户=0;
ArrayList list=新的ArrayList();
HashMap finalmap=新HashMap();
ArrayList日期=新建ArrayList();
for(用户时间:myEmpls)
{
if(prevUser==time.getEmpcode())
{
users=time.getEmpcode();
System.out.println(“用户”+用户);
dates.add(time.getDate());
}
其他的
{
dates.add(time.getDate());
}
系统输出打印项次(“日期”+日期);
最终地图放置(用户、列表);
prevUser=time.getEmpcode();
cnt++;
}
有人能帮我解决这个问题吗?

Map-Map=newhashmap();
Map<Long,ArrayList<String>> map=new HashMap<Long,ArrayList<String>>();
public void addToMap(long id,String blaa)
{
     ArrayList<String> ar=map.get(id)
     if(ar==null)
     {
          ar=new ArrayList<String>();
          map.put(id,ar);
     }
     ar.add(blaa);
}
public void addToMap(长id,字符串blaa) { ArrayList ar=map.get(id) if(ar==null) { ar=新的ArrayList(); 地图放置(id,ar); } ar.add(blaa); }
这是你想要的吗?只需为收到的每一行调用此函数

Map Map=new HashMap();
public void addToMap(长id,字符串blaa)
{
ArrayList ar=map.get(id)
if(ar==null)
{
ar=新的ArrayList();
地图放置(id,ar);
}
ar.add(blaa);
}

这是你想要的吗?只需对收到的每一行调用此命令即可

使用类似以下内容:

  HashMap<Long, ArrayList> finalmap =  new HashMap<Long, ArrayList>();
 for(User obj : myEmpls){

        if(finalmap.containsKey(obj.getEmpcode())){
            ArrayList<String> dates = finalmap.get(obj.getEmpcode());
            dates.add(obj.getDate());
            finalmap.put(obj.getEmpcode(), dates);
        }else{
            ArrayList<String> dates = new ArrayList<String>();
            dates.add(obj.getDate());
            finalmap.put(obj.getEmpcode(), dates);
        }

    }
HashMap finalmap=newhashmap();
for(用户对象:myEmpls){
if(finalmap.containsKey(obj.getEmpcode())){
ArrayList dates=finalmap.get(obj.getEmpcode());
add(obj.getDate());
finalmap.put(obj.getEmpcode(),日期);
}否则{
ArrayList日期=新建ArrayList();
add(obj.getDate());
finalmap.put(obj.getEmpcode(),日期);
}
}

使用类似以下内容:

  HashMap<Long, ArrayList> finalmap =  new HashMap<Long, ArrayList>();
 for(User obj : myEmpls){

        if(finalmap.containsKey(obj.getEmpcode())){
            ArrayList<String> dates = finalmap.get(obj.getEmpcode());
            dates.add(obj.getDate());
            finalmap.put(obj.getEmpcode(), dates);
        }else{
            ArrayList<String> dates = new ArrayList<String>();
            dates.add(obj.getDate());
            finalmap.put(obj.getEmpcode(), dates);
        }

    }
HashMap finalmap=newhashmap();
for(用户对象:myEmpls){
if(finalmap.containsKey(obj.getEmpcode())){
ArrayList dates=finalmap.get(obj.getEmpcode());
add(obj.getDate());
finalmap.put(obj.getEmpcode(),日期);
}否则{
ArrayList日期=新建ArrayList();
add(obj.getDate());
finalmap.put(obj.getEmpcode(),日期);
}
}

像那样做吧。。。更简单的是:

class User {
    private Long id;
    private String date;

    public User(Long id, String date) {
        this.id = id;
        this.date = date;
    }

    public Long getId() {
        return this.id;
    }

    public String getDate() {
        return this.date;
    }
}

List<User> listUsers = new ArrayList<User>();
listUsers.add(new User(new Long(2500), "03/05/2013"));
listUsers.add(new User(new Long(2500), "04/05/2013"));
listUsers.add(new User(new Long(2500), "05/05/2013"));
listUsers.add(new User(new Long(200), "10/05/2013"));
listUsers.add(new User(new Long(200), "18/05/2013"));

HashMap<Long, ArrayList> map = new HashMap<Long, ArrayList>();

for(User user : listUsers) {        
    if(map.containsKey(user.getId())) {
        map.get(user.getId()).add(user.getDate());
    } else {
        ArrayList<String> dates = new ArrayList<String>();
        dates.add(user.getDate());
        map.put(user.getId(), dates);
    }
}

//just to check
System.out.println("number of keys : " + map.size());
System.out.println("number of dates for 2500 : " + map.get(new Long(2500)).size());
System.out.println("number of dates for 200 : " + map.get(new Long(200)).size());
类用户{
私人长id;
私有字符串日期;
公共用户(长id,字符串日期){
this.id=id;
this.date=日期;
}
公共长getId(){
返回此.id;
}
公共字符串getDate(){
返回此日期;
}
}
List listUsers=new ArrayList();
添加(新用户(新长(2500),“03/05/2013”);
添加(新用户(新长(2500),“04/05/2013”);
添加(新用户(新长(2500),“2013年5月5日”);
添加(新用户(新长(200),“10/05/2013”);
添加(新用户(新长(200),“18/05/2013”);
HashMap=newHashMap();
对于(用户:listUsers){
if(map.containsKey(user.getId())){
map.get(user.getId()).add(user.getDate());
}否则{
ArrayList日期=新建ArrayList();
dates.add(user.getDate());
map.put(user.getId(),日期);
}
}
//只是检查一下
System.out.println(“键数:+map.size());
System.out.println(“2500的日期数:”+map.get(new Long(2500)).size());
System.out.println(“200的日期数:”+map.get(new Long(200)).size());

像那样做吧。。。更简单的是:

class User {
    private Long id;
    private String date;

    public User(Long id, String date) {
        this.id = id;
        this.date = date;
    }

    public Long getId() {
        return this.id;
    }

    public String getDate() {
        return this.date;
    }
}

List<User> listUsers = new ArrayList<User>();
listUsers.add(new User(new Long(2500), "03/05/2013"));
listUsers.add(new User(new Long(2500), "04/05/2013"));
listUsers.add(new User(new Long(2500), "05/05/2013"));
listUsers.add(new User(new Long(200), "10/05/2013"));
listUsers.add(new User(new Long(200), "18/05/2013"));

HashMap<Long, ArrayList> map = new HashMap<Long, ArrayList>();

for(User user : listUsers) {        
    if(map.containsKey(user.getId())) {
        map.get(user.getId()).add(user.getDate());
    } else {
        ArrayList<String> dates = new ArrayList<String>();
        dates.add(user.getDate());
        map.put(user.getId(), dates);
    }
}

//just to check
System.out.println("number of keys : " + map.size());
System.out.println("number of dates for 2500 : " + map.get(new Long(2500)).size());
System.out.println("number of dates for 200 : " + map.get(new Long(200)).size());
类用户{
私人长id;
私有字符串日期;
公共用户(长id,字符串日期){
this.id=id;
this.date=日期;
}
公共长getId(){
返回此.id;
}
公共字符串getDate(){
返回此日期;
}
}
List listUsers=new ArrayList();
添加(新用户(新长(2500),“03/05/2013”);
添加(新用户(新长(2500),“04/05/2013”);
添加(新用户(新长(2500),“2013年5月5日”);
添加(新用户(新长(200),“10/05/2013”);
添加(新用户(新长(200),“18/05/2013”);
HashMap=newHashMap();
对于(用户:listUsers){
if(map.containsKey(user.getId())){
map.get(user.getId()).add(user.getDate());
}否则{
ArrayList日期=新建ArrayList();
dates.add(user.getDate