我以这种方式获取数据库
1125,3-05-2013
1125,4-05-2013
1125,5-05-2013
200,23-05-2013
200,24-05-2013
我需要将这些添加到hashmap中作为hashmap(userid,dates)..
即:hashmap(long,arraylist(日期字符串))并发送到前端。
我的意思是hashmap中的long值应该是唯一的,这是检索特定用户id的所有日期列表的关键,
所以如果我尝试hashmap.get(1125) == i
应该获得用户1125的所有日期列表,如3-05-2013,4-05-2013,5-05-2013
然后如果我尝试hashmap.get(200) == i
应该获得用户200的所有日期列表,如23-05-2013,24-05-2013
我试过这种方式,但我得到的是单个用户ID的所有日期,例如
users200 日期[3-05-2013,4-05-2013,5-05-2013,23-05-2013,24-05-2013]
这是我的代码,
// 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++;
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
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);
}
这是你想要的吗?只需为您收到的每一行打电话
答案 1 :(得分:1)
这样做......更简单:
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());
答案 2 :(得分:-1)
使用类似的东西:
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);
}
}