java 将长键和数组列表值添加到哈希映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16815814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding long key and arraylist value to hashmap
提问by user1660325
I am getting db data this way
我以这种方式获取数据库数据
userid, dates
用户名、日期
1125,3-05-2013
1125,3-05-2013
1125,4-05-2013
1125,4-05-2013
1125,5-05-2013
1125,5-05-2013
200,23-05-2013
200,23-05-2013
200,24-05-2013
200,24-05-2013
I need to add these to hashmap as hashmap(userid,dates)..
我需要将这些作为 hashmap(userid,dates) 添加到 hashmap 中。
i.e: hashmap(long,arraylist(string of dates)) and send to front end.
即: hashmap(long,arraylist(string of date)) 并发送到前端。
I mean long value in hashmap should be unique, which is an key to retrive list of all dates for a particular user id,
我的意思是 hashmap 中的 long 值应该是唯一的,这是检索特定用户 ID 的所有日期列表的关键,
so if I try hashmap.get(1125) == i
should get list of all dates for user 1125 like 3-05-2013,4-05-2013,5-05-2013
所以如果我尝试hashmap.get(1125) == i
应该获取用户 1125 的所有日期列表,例如 3-05-2013,4-05-2013,5-05-2013
then if I try hashmap.get(200) == i
should get list of all dates for user 200 like 23-05-2013,24-05-2013
那么如果我尝试 hashmap.get(200) == i
应该获取用户 200 的所有日期列表,例如 23-05-2013,24-05-2013
I tried this way , but I am getting all the dates for single userid like,
我试过这种方式,但我得到了单个用户 ID 的所有日期,例如,
users200 dates[3-05-2013, 4-05-2013, 5-05-2013, 23-05-2013, 24-05-2013]
users200 日期[3-05-2013, 4-05-2013, 5-05-2013, 23-05-2013, 24-05-2013]
Here is my code,
这是我的代码,
// 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++;
}
can some one help me in this issue?
有人可以帮助我解决这个问题吗?
回答by Markus Mikkolainen
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);
}
is this what you want? just call this for each row you receive
这是你想要的吗?只需为您收到的每一行调用它
回答by mlwacosmos
Do like that... it is more simple :
这样做...更简单:
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());
回答by Satya
Use something like this:
使用这样的东西:
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);
}
}